Python common functions and formatted output

Time:2021-12-4

Case conversion method

upper()

Used to convert lowercase letters to uppercase letters in a string.

'abcd'.upper()  # 'ABCD'
'aBcD'.upper()  # 'ABCD'

lower()

Used to convert uppercase letters to lowercase letters in a string.

'ABCD'.lower()  # 'abcd'
'aBcD'.lower()  # 'abcd'

capitalize()

Used to make the first letter of a string uppercase and the other letters lowercase. If the first character is a non letter, it remains unchanged, and other letters become lowercase.

'ABCD'.capitalize()  # 'Abcd'
'aBcD'.capitalize()  # 'Abcd'
'1abcd'.capitalize()  # '1abcd'
'1aBcD'.capitalize()  # '1abcd'

title()

Used to return a “captioned” string – that is, the first letter of all words is capitalized and the rest are lowercase.

  • Note: the first letter after a non letter will be capitalized.
'this is an example string'.title()   # This Is An Example String
'5g is coming'.title()  # 5G Is Coming

swapcase()

Used to convert uppercase and lowercase letters of strings to each other.

'abcd'.swapcase()  # 'ABCD'
'aBcD'.swapcase()  # 'AbCd'

Let’s summarize:

Python common functions and formatted output

Segmentation, combination and removal methods

split()

Used to split a string by specifying a delimiter. The split() method has two optional parameters. The first parameter is a separator, which defaults to all empty characters, including spaces, line breaks (\ n), tabs (\ T), etc. The second parameter is the maximum number of divisions, which is – 1 by default, that is, unlimited times.

'scallop programming '. Split() # [' scallop ',' scallop ',' programming ',' process']
'scallop programming '. Split (' ') # [' scallop ',' scallop ',' programming ',' process']
'fan Bei Bian Cheng'. Split ('-') # ['fan', 'Bei', 'Bian', 'Cheng']
'scallop programming '. Split ('scallop programming') # ['fan', 'process']
'scallop programming '. Split (', 1) # ['scallop', 'scallop programming']

join()

Used to connect the elements in the sequence with the specified characters to generate a new string. The join () method takes a sequence (list, tuple, string, etc.) as a parameter,.The preceding string is used for the connector.

#List
''. Join (['fan', 'Bei', 'Edit', 'process']) # scallop programming
'-'. Join (['fan', 'Bei', 'Bian', 'Cheng']) # fan Bei Bian Cheng
#Tuple
'-'. Join (('fan', 'Bei', 'Bian', 'Cheng') # fan Bei Bian Cheng
#String
'-'. Join ('scallop programming ') # fan scallop programming

strip()

Removes the character (null by default) or character sequence specified at the beginning and end of a string. When a string is passed in, each character in the incoming string is removed in turn.

'scallop programming '. Strip() #' scallop programming '
'~ ~ scallop programming ~ ~'. Strip ('~') # 'scallop programming'
'~ ~ scallop ~ ~ programming ~ ~'. Strip ('~') # 'scallop ~ ~ programming'
'_~_ Scallop programming ~ ~ ' Strip ('~ #') # 'scallop programming'
Python common functions and formatted output

Positioning and replacement methods

count()

Used to count the number of occurrences of a string in a string. The first parameter is the string to count the number of times, and the last two optional parameters are the index at the beginning and end of string search.

'aabbcccd'.count('a')  # 2
'aabbcccd'.count('ab')  # 1
'aabbcccd'.count('e')  # 0
'aabbcccd'.count('a', 2, 6)  # 0
#Equivalent to 'BBCC'. Count ('a ')
'aabbcccd'.count('c', 2, 6)  # 2
#Equivalent to 'BBCC'. Count ('c ')

find()

Used to detect whether the string contains a substring. If it contains a substring, the index value of the first occurrence is returned; otherwise, – 1 is returned. The first parameter is the substring to find, and the last two optional parameters are the index at the beginning and end of string search.

'abc'.find('b')  # 1
'abcabc'.find('b')  # 1
'abcabc'.find('d')  # -1
'abcbcdabc'.find('bcd')  # 3
'abcabc'.find('b', 1, 2)  # 1
'abcabc'.find('b', 2, 3)  # -1

replace()

Used to replace the specified string in the string with another string. The first parameter is the replaced string, the second parameter is the replacement string, and the third optional parameter is the maximum number of replacements, which is infinite by default.

'abcd'.replace('b', 'e')  # 'aecd'
'abbbcbd'.replace('b', 'e')  # 'aeeeced'
'abbbcbd'.replace('bb', 'e')  # 'aebcbd'
'abbbcbd'.replace('b', 'e', 2)  # 'aeebcbd'
Python common functions and formatted output

Format output method

In addition to using the% operator for string formatted output, we can also use the format () method of string to achieve the same effect.

'My name is {}, I'm {} years old, from {} '. Format ('xiaobei', 18, 'Nanjing')
#My name is Xiao Bei. I am 18 years old and come from Nanjing
Python common functions and formatted output

As you can see from the above example, the format () method uses {} as a placeholder, and then replaces the corresponding placeholders in the order of parameters.

We can also number placeholders:

'My name is {0}, I'm {1} years old, from {2} '. Format ('xiaobei', 18, 'Nanjing')
#My name is Xiao Bei. I am 18 years old and come from Nanjing
#Adjust numbering sequence
'My name is {2}, I'm {1} years old, from {0} '. Format ('nanjing', 18, 'Xiaobei')
#My name is Xiao Bei. I am 18 years old and come from Nanjing
#Repeat number
'My name is {0}, {0} I'm {1} years old, from {2} '. Format ('xiaobei', 18, 'Nanjing')
#My name is Xiao Bei. Xiao Bei is 18 years old and comes from Nanjing

We can also name the placeholder:

'My name is {name}, I'm {age} years old, from {City} '. Format (name =' Xiaobei ', age = 18, city =' Nanjing ')
#My name is Xiao Bei. I am 18 years old and come from Nanjing

#After naming, the order of parameters is no longer important
'My name is {name}, I'm {age} years old, from {City} '. Format (age = 18, city =' Nanjing ', name =' Xiaobei ')
#My name is Xiao Bei. I am 18 years old and come from Nanjing