在python中如何将一位数转换为两位数的字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3505831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:29:24  来源:igfitidea点击:

in python how do I convert a single digit number into a double digits string?

pythonstringnumbersdigits

提问by Joe Schmoe

So say i have

所以说我有

a = 5

一 = 5

i want to print it as a string '05'

我想将它打印为字符串 '05'

采纳答案by jkerian

print "%02d"%ais the python 2 variant

print "%02d"%a是 python 2 变体

python 3 uses a somewhat more verbose formatting system:

python 3 使用了更冗长的格式系统:

"{0:0=2d}".format(a)

The relevant doc link for python2 is: http://docs.python.org/2/library/string.html#format-specification-mini-language

python2的相关文档链接是:http: //docs.python.org/2/library/string.html#format-specification-mini-language

For python3, it's http://docs.python.org/3/library/string.html#string-formatting

对于 python3,它是http://docs.python.org/3/library/string.html#string-formatting

回答by user225312

>>> print '{0}'.format('5'.zfill(2))
05

Read more here.

在这里阅读更多。

回答by tux21b

a = 5
print '%02d' % a
# output: 05

The '%' operator is called string formattingoperator when used with a string on the left side. '%d'is the formatting code to print out an integer number (you will get a type error if the value isn't numeric). With '%2dyou can specify the length, and '%02d'can be used to set the padding character to a 0 instead of the default space.

'%' 运算符在与左侧的字符串一起使用时称为字符串格式化运算符。'%d'是打印出整数的格式代码(如果该值不是数字,则会出现类型错误)。用'%2d你可以指定长度,并且'%02d'可以用来将填充字符设置为 0 而不是默认空格。

回答by Mohammad Shahid Siddiqui

>>> a=["%02d" % x for x in range(24)]
>>> a
['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23']
>>> 

It is that simple

就是这么简单

回答by Sarvesh Chitko

In Python3, you can:

在 Python3 中,您可以:

print("%02d" % a)

回答by Alex Schwab

Branching off of Mohommad's answer:

分支穆罕默德的回答:

str_years = [x for x in range(24)]
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]

#Or, if you're starting with ints:
int_years = [int(x) for x in str_years]

#Formatted here
form_years = ["%02d" % x for x in int_years]

print(form_years)
#['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23']

回答by nishant thakkar

df["col_name"].str.rjust(4,'0')#(length of string,'value') --> ValueXXX --> 0XXX  
df["col_name"].str.ljust(4,'0')#(length of string,'value') --> XXXValue --> XXX0

回答by MountainWaterProducts

If you are an analyst and not a full stack guy, this might be more intuitive:

如果您是分析师而不是全栈人员,这可能更直观:

[(str('00000') + str(i))[-5:] for i in arange(100)]

breaking that down, you:

打破它,你:

  • start by creating a list that repeats 0's or X's, in this case, 100 long, i.e., arange(100)

  • add the numbers you want to the string, in this case, numbers 0-99, i.e., 'i'

  • keep only the right hand 5 digits, i.e., '[-5:]' for subsetting

  • output is numbered list, all with 5 digits

  • 首先创建一个重复 0 或 X 的列表,在这种情况下,长度为 100,即 arange(100)

  • 将您想要的数字添加到字符串中,在本例中为数字 0-99,即 'i'

  • 只保留右手边的 5 位数字,即 '[-5:]' 用于子集化

  • 输出是编号列表,全部为 5 位数字

回答by K Puri

This is a dumb solution but I was getting type errors with the other solutions above. So if all else fails, yolo:

这是一个愚蠢的解决方案,但我在使用上述其他解决方案时遇到了类型错误。所以如果所有其他方法都失败了,yolo:

images3digit = []

for i in images:
        if len(i)==1:
            i = '00'+i
            images3digit.append(i)
        elif len(i)==2:
            i = '0'+i
            images3digit.append(i)
        elif len(i)==3:
            images3digit.append(i)