在字符串上左边填充零... [英] left padding zeroes on a string...
问题描述
嘿所有:
我想将字符串(例如3,32)转换成带左边填充的字符串
零(例如''003'','032''),所以我试过这个:
string1 =''32''
string2 ="%03s" %(string1)
print string2
32
这不起作用。如果我将string1作为int转换它是有效的:
string1 =''32''
int2 ="%03d" %(int(string1))
print int2
032
当然,我需要施放结果返回一个字符串来使用它。为什么
不是第一个工作的例子?
-cjl
cjl写道:嘿所有:
我想将字符串(例如3,32)转换为左边填充的字符串
零(例如''003'','032''),所以我试过了:
string1 ='''32''
string2 ="%03s" %(string1)
string1.zfill(3)
-Peter
" CJL" < CJ **** @ gmail.com>写道:
嘿所有:
我想将字符串(例如3,32)转换为带左边填充的字符串
零(例如''003'','032''),所以我试过了:
string1 =''32''
string2 ="% 03S" %(string1)
print string2
32
这不行。
实际上在这种情况下,string2用空格填充,而不是零。
如果我将string1作为int转换它是有效的:
string1 ='''32''> int2 ="%03d" %(int(string1))
print int2
032
当然,我需要将结果强制转换为字符串才能使用它。为什么
不是第一个例子吗?
这是不对的; int2是一个字符串,所以你可以直接使用它(并且可能将它重命名为
更合适)。
-cjl
>
问候,
乔治
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~
"只有当它在普通牛群的ken范围内才能看到胜利不是
卓越的极致。
孙子,''战争的艺术''
~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ blockquote>
你的第一次转换工作正常。
string1 =''32''
string2 ="%04s" %(string1)
print string2
''32''
请注意,它返回一个在左侧填充空格的字符串。 />
如果你想填写左边0'的数字你需要使用
zfill()
''32''。 zfill(4)
''0032''
一定要学习字符串方法,它会节省你的时间和
理智。
Py> dir('''')
[''__add__'',''__ class__'',''_ _ _ _ _ _ _'''''''''__ delattr _''''''__ doc__'','
''__ eq__'',''__ ge__'',''_ _ getattribute __'','__ getitem__'',''_ _ _ _ _ _ _ _ _''''''','
''__ gt__'','' __hash__'',''__ init__'',''__ le__'',''_ _ _ _ _ _'',''__ lt__'',
''__ mul__'',''__ ne__'','' __new__'',''__ reduce__'',''_ _ _ _ _ _ _ _'''''''''''''''''''_'''''''''''''''_''''''''_'''''''''_'''''''''''''''''''''''''''''中心'',''计数'',''解码'',
''编码'',''结束'',''expandtabs'',''find'','' index'',''isalnum'',
''isalpha'',''isdigit'',''islower'',''isspace'',''istitle'','' isupper'',
''加入'',' ljust'',''lower'',''lstrip'','''替换'',''rfind'',''rindex'',
''rjust'','' rstrip'',''split'','splitlines'',''startswith'',''strip'',
''swapcase'',''title'',''翻译'',''上'',''zfill'']
Py> help(''''。zfill)
内置函数zfill的帮助:
zfill(...)
S.zfill(宽度) - >字符串
在左侧填充一个带有零的数字字符串S,以填充指定宽度的字段
。字符串S永远不会被截断。
Hth,
M.E.Farmer
Hey all:
I want to convert strings (ex. ''3'', ''32'') to strings with left padded
zeroes (ex. ''003'', ''032''), so I tried this:
string1 = ''32''
string2 = "%03s" % (string1)
print string2
32
This doesn''t work. If I cast string1 as an int it works:
string1 = ''32''
int2 = "%03d" % (int(string1))
print int2
032
Of course, I need to cast the result back to a string to use it. Why
doesn''t the first example work?
-cjl
解决方案cjl wrote:Hey all:
I want to convert strings (ex. ''3'', ''32'') to strings with left padded
zeroes (ex. ''003'', ''032''), so I tried this:
string1 = ''32''
string2 = "%03s" % (string1)
string1.zfill(3)
-Peter
"cjl" <cj****@gmail.com> wrote:
Hey all:
I want to convert strings (ex. ''3'', ''32'') to strings with left padded
zeroes (ex. ''003'', ''032''), so I tried this:
string1 = ''32''
string2 = "%03s" % (string1)
print string232
This doesn''t work.
Actually in this case string2 is padded with spaces, instead of zeros.
If I cast string1 as an int it works:
string1 = ''32''
int2 = "%03d" % (int(string1))
print int2032
Of course, I need to cast the result back to a string to use it. Why
doesn''t the first example work?
That''s not correct; int2 is a string so you can use it directly (and probably rename it to something
more appropriate).
-cjl
Regards,
George
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To see victory only when it is within the ken of the common herd is not
the acme of excellence."
Sun Tzu, ''The Art of War''
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Your first conversion works fine.
string1 = ''32''
string2 = "%04s" % (string1)
print string2
'' 32''
Notice that it returns a string with spaces padding the left side.
If you want to pad a number with 0''s on the left you need to use
zfill()
''32''.zfill(4)
''0032''
Be sure to study up on string methods, it will save you time and
sanity.
Py> dir('''')
[''__add__'', ''__class__'', ''__contains__'', ''__delattr__'', ''__doc__'',
''__eq__'', ''__ge__'', ''__getattribute__'', ''__getitem__'', ''__getslice__'',
''__gt__'', ''__hash__'', ''__init__'', ''__le__'', ''__len__'', ''__lt__'',
''__mul__'', ''__ne__'', ''__new__'', ''__reduce__'', ''__repr__'', ''__rmul__'',
''__setattr__'', ''__str__'', ''capitalize'', ''center'', ''count'', ''decode'',
''encode'', ''endswith'', ''expandtabs'', ''find'', ''index'', ''isalnum'',
''isalpha'', ''isdigit'', ''islower'', ''isspace'', ''istitle'', ''isupper'',
''join'', ''ljust'', ''lower'', ''lstrip'', ''replace'', ''rfind'', ''rindex'',
''rjust'', ''rstrip'', ''split'', ''splitlines'', ''startswith'', ''strip'',
''swapcase'', ''title'', ''translate'', ''upper'', ''zfill'']
Py> help(''''.zfill)
Help on built-in function zfill:
zfill(...)
S.zfill(width) -> string
Pad a numeric string S with zeros on the left, to fill a field
of the specified width. The string S is never truncated.
Hth,
M.E.Farmer
这篇关于在字符串上左边填充零...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!