1. String case conversion
value = "wangdianchao" # Convert to uppercase big_value = () print(big_value) # Convert to lowercase small_value = big_value.lower() print(small_value)
2. Determine whether the input string can be converted to a number
num = input("Input content:") # Determine if an input string can be converted to a number flag = () print(flag)
3. Remove spaces from strings
user = input("Please enter user name:") # Remove spaces from the right side of the string new_user = () print(new_user) user = input("Please enter user name:") # Remove spaces from the left side of the string new_user = () print(new_user) user = input("Please enter user name:")# Remove spaces or newlines on both sides of a stringnew_user = ()print(new_user)
4. Replace the characters in the string
message = input("Please enter a message:") # Replace characters in the string (replace the word "moncler" with "**" in the input message) data = ('Grandpa','**') print(data) message = input("Please enter a message:") # Replace only the first character in the string data = ('Grandpa','**',1) print(data) message = input("Please enter a message:") # Replace the first two characters in a string data = ('Grandpa','**',2) print(data)
5. Cutting strings
message = "As the early morning rays of sunlight reflected through the gaps in the curtains on sleeping faces, slightly opened eyes gazed hazily at everything around them as a new day crept in." # Cut strings to specific characters data = (',') print(data) message = "As the early morning rays of sunlight reflected through the gaps in the curtains on sleeping faces, slightly opened eyes gazed hazily at everything around them as a new day crept in." # Cut the string 1 time based on specific characters data = (',',1) print(data) message = "As the early morning rays of sunlight reflected through the gaps in the curtains on sleeping faces, slightly opened eyes gazed hazily at everything around them as a new day crept in." # Cut the string 1 time from the right side according to a specific character cut data = (',',1) print(data)
6. Check if the string starts with the specified substring
str = "this is string example....wow!!!" # The Python startswith() method is used to check whether a string begins with the specified substring, returning True if it does and False otherwise. print(('this')) # 2 is an optional parameter used to set the starting position for string detection. # 4 is an optional parameter to set the end position of the string detection. print(('is', 2, 4)) print(('this', 2, 4))
7. Determine whether the string ends with the specified suffix
str = "this is string example....wow!!!" suffix = "wow!!!" # Determine whether the string ends with the specified suffix, if it ends with the specified suffix return True, otherwise return False. print((suffix)) print((suffix, 20)) suffix = "is" # 2 indicates the start position in the string # 4 indicates the end position in the string print((suffix,2,4)) print((suffix,2,6))
8. String formatting
str = "website name:{name}, address {url}" # Format the characters in the above string {} into the desired characters print((name="Baidu.", url=))
9. Change the string encoding format
str = "this is string example....wow!!!" # Change string encoding format print(('utf-8'))
10. The elements of the sequence in the specified character concatenation to generate a new string
str = "-" seq = ("a", "b", "c") # The elements in the string sequence must be strings # Generate a new string by concatenating the elements of the sequence seq with the str string. print((seq))
11. Convert between string and binary
data = "Wang Tsuen-chao # Convert strings to binary new_data = ('utf-8') # Convert binary codes to strings yhf = new_data.decode('utf-8') print(yhf)
This is the whole content of this article.