Python String operations

Int to String
pyStr = str(10)

String append
"hello" + "world"

Length
pyStr = "GoodBye"
len(pyStr)
>>> 7

Substring
pyStr[:-1]
>>> GoodBy

pyStr[1:4]
>>> ood

Split
pyStr = "Hello world everybody"
segment = pyStr.split(" ")
>>> ['Hello', 'world', 'everybody']


digit   alpha   
x = "123"
x.isdigit()
>>> True
x.isalpha()
>>> False

lower  upper
x = "M"
x.islower()
>>> False
x.isupper()
>>> True

%
str ="%s is %s " % ( "Apple", "Fruit")
>>> Apple is Fruit

format
'We are the {} who say "{}!"'.format('knights', 'Ni')
>>> We are the knights who say "Ni!"

0 意見: