Strings¶
Create string¶
s = str()
Concatenate¶
s1 = str("hello ")
s2 = str("world")
print(s1 + s2)
Format¶
my_string = "My name is {0} {1} and I'm {2} years old.".format(first_name, name, age)
my_string = "My name is {fn} {n} and I'm {a} years old.".format(fn=first_name, n=name, a=age)
my_string = "My name is %s %s and I'm %i years old." % (first_name, name, age)
Join strings with a defined separator : join()
¶
numList = ['1', '2', '3', '4']
seperator = ' - '
print(seperator.join(numList))
-> output :
1 - 2 - 3 - 4
With a list of separators :
s1 = 'abc'
s2 = '123'
#Each character of s2 is concatenated to the front of s1
print('s1.join(s2):', s1.join(s2))
#Each character of s1 is concatenated to the front of s2
print('s2.join(s1):', s2.join(s1))
-> output :
s1.join(s2): 1abc2abc3 s2.join(s1): a123b123c
Create a list from a string, split a a sepecific separator : split()
¶
ma_chaine = "Bonjour à tous"
ma_chaine.split(" ")
# -> ['Bonjour', 'à', 'tous']
Lower case¶
s.lower()
Upper case¶
s.upper()
Capitalize first character¶
s.capitalize()