1. What is string built-in function
As long as a string is created, built-in functions (some functions prepared by the system) can be called by default.
2. Case related
The main functions are capitalization(), title(), upper(), lower(), etc.
-
capitalize(): capitalize the first letter of the string
message = 'you are a beautiful girl!' msg = message.capitalize() print(msg) #The output is: 'You are a beautiful girl!'
-
title(): capitalize each word
message = 'you are a beautiful girl!' msg = message.title() print(msg) #The output is: 'You Are A Beautiful Girl!'
-
istitle(): determines whether the initial of each word is uppercase, and the return value is boolean type
message = 'you are a beautiful girl!' msg = message.istitle() print(msg) #Output: False
-
upper(): converts all strings to uppercase
message = 'you are a beautiful girl!' msg = message.upper() print(msg) #The output is: 'YOU ARE A BEAUTIFUL GIRL'
-
lower(): converts all uppercase letters of a string to lowercase
message = 'You are a beautiful Girl!' msg = message.upper() print(msg) #The output is: 'you are a beautiful girl!'
3. Find and replace
It mainly includes find(), rfind(), lfind(), index(), rindex(), lindex(), replace(), etc.
-
find(): returns the position of the character (string). If it is not found, the return value is - 1. If it can be found, the position where the letter (string) first appears will be returned.
Function format: find('character to find ', start, end), start is to specify the starting position and end is to specify the ending position.message = 'you are a beautiful girl!' position = message.find('a') print(position) #Output: 4
-
rfind() & lfind()
- rfind(): indicates that retrieval starts on the right side
- lfind(): indicates that retrieval starts on the left side
- lfind() and rfind() are the same as find()
-
index() & rindex() & lindex()
Index(), rindex(), and lindex() are basically the same as the corresponding find(), rfind(), and lfind() functions, except for functions such as index(). If the str searched is not in the string, an exception will be reported, and find() functions will return - 1. -
Example: print file name
url = 'https://www.baidu.com/img/bg_1.jpg' p = url.rfind('/') filename = url[p+1:] print(filename)
-
replace(): the function prototype is replace(old,new,[max]), where [max] means optional and max means at most several replacements. Old is the string to be replaced, and new is the string to be replaced.
4. Encoding and decoding
Encoding and decoding are embodied in network transmission. Chinese characters usually involve encoding. The encoding format is the international standard of utf-8 by default in Python 3.
#Code msg = 'It's nice of you to laugh.' result = msg.encode('utf-8') #Decode m = result.decode('utf-8') print(m)
5. Judge beginning and end
The functions are startswitch(), endswitch(), and the return values are all Boolean types.
- Startswitch(): judgment starts with xxx
- Endswitch(): judge whether to end with xxx
# Application: file upload filename = 'note.docx' # Determine if filename ends with docx result = filename.endswith('docx') print(result)
6. Judge whether it is a number
The main functions are: isalpha(), isdigit(), and the returned result is a Boolean value.
-
isalpha(): judge whether it is all letters
s = 'efgc7' result = s.isalpha() print('result=',result) #False result
-
isdigit(): judge whether all are numbers
s = '7799' result = s.isdigit() print(result) #Result is True
7,join,split,count
-
join() function
new_str = '-'.join('abc') print(new_str) #The output is' a-b-c ' list = ['J','a','s','o','n'] result = ''.join(list) print(result) #The output is' Jason '
-
split() function
-
lstrip()
s = ' hi ' s = s.lstrip() #Remove space to the left of string print(s+'8') #Output result is' hi 8 '
-
rstrip()
s = 'hi ' s = s.rstrip() #Remove space on right print(s) #Output result is' hi '
-
strip()
Executing this function is equivalent to executing both lstrip() and rstrip() functions.
-
-
count() function
Find the number of args specified in the string.s = 'Your name is Jason' n = s.count('a') print(n) #Output: 2