String¶
Snippets on string manipulation.
check if a string is a number¶
>>> a = "123"
>>> a.isdigit()
True
>>> b = "123abc"
>>> b.isdigit()
False
reverse a string¶
>>> 'hello world'[::-1]
'dlrow olleh'
write long string¶
If you follow PEP8, there is a maximum of 79 characters limit, BTW, you should follow PEP8, :)
string = ("this is a "
"really long long "
"string")
number with leading zeros¶
>>> "%02d" % 1
'01'
>>> "%02d" % 100
'100'
>>> str(1).zfill(2)
'01'
>>> str(100).zfill(2)
'100'
>>> int('01')
1