String Formatting Easiest Prettiest
本问题已经有最佳答案,请猛点这里访问。
我发现它需要相当多的关注点,用我当前使用的语法来格式化字符串需要一定的时间:
1 2 | myList=['one','two','three'] myString='The number %s is larger than %s but smaller than %s.'%(myList[1],myList[0],myList[2]) |
结果:
1 | "The number two is larger than one but smaller than three" |
奇怪的是,每当我到达EDCOX1,0键盘键,接着EDCX1,1,我感觉有点被打断…
我想知道是否有其他方法可以实现类似的字符串格式。请张贴一些例子。
您可能正在寻找
1 2 3 4 | >>> myList=['one','two','three'] >>> 'The number {1} is larger than {0} but smaller than {2}.'.format(*myList) 'The number two is larger than one but smaller than three.' >>> |
这种方法的主要优点是,不必执行
也请注意,如果
1 2 3 4 | >>> myList=['two','one','three'] >>> 'The number {} is larger than {} but smaller than {}.'.format(*myList) 'The number two is larger than one but smaller than three.' >>> |