Comprehensive Summary of Common Python String Methods

Follow and star to learn new Python skills every day

Due to changes in the public account’s push rules, please click “View” and add “Star” to get exciting technical shares at the first time

Source: Internet

1. String Case Conversion

value = "wangdianchao"# Convert to uppercasebig_value = value.upper()print(big_value)# Convert to lowercasesmall_value = big_value.lower()print(small_value)

2. Check if the input string can be converted to a number

num = input("Enter content:")# Check if the input string can be converted to a numberflag = num.isdigit()print(flag)

3. Remove spaces from the string

user = input("Please enter username:")# Remove spaces from the rightnew_user = user.rstrip()print(new_user)user = input("Please enter username:")# Remove spaces from the leftnew_user = user.lstrip()print(new_user)user = input("Please enter username:")# Remove spaces or newlines from both sidesnew_user = user.strip()print(new_user)

4. Replace characters in the string

message = input("Please enter information:")# Replace characters in the string (replace "大爷" with "**")data = message.replace('大爷','**')print(data)message = input("Please enter information:")# Replace only the first occurrence of the characterdata = message.replace('大爷','**',1)print(data)message = input("Please enter information:")# Replace the first two occurrences of the characterdata = message.replace('大爷','**',2)print(data)

5. Split the string

message = "When a ray of morning sunlight shines through the gap in the curtain onto the sleeping face, the slightly opened eyes hazily gaze at everything around, a new day quietly arrives."# Split the string by a specific characterdata = message.split(',')print(data)message = "When a ray of morning sunlight shines through the gap in the curtain onto the sleeping face, the slightly opened eyes hazily gaze at everything around, a new day quietly arrives."# Split the string by a specific character oncedata = message.split(',',1)print(data)message = "When a ray of morning sunlight shines through the gap in the curtain onto the sleeping face, the slightly opened eyes hazily gaze at everything around, a new day quietly arrives."# Split the string from the right oncedata = message.rsplit(',',1)print(data)

6. Check if the string starts with a specified substring

str = "this is string example....wow!!!"# The Python startswith() method checks if the string starts with the specified substring, returning True if it does, otherwise False.print(str.startswith('this'))# 2 is an optional parameter to set the starting position for the string check.# 4 is an optional parameter to set the ending position for the string check.print(str.startswith('is', 2, 4))print(str.startswith('this', 2, 4))

7. Check if the string ends with a specified suffix

str = "this is string example....wow!!!"suffix = "wow!!!"# Check if the string ends with the specified suffix, returning True if it does, otherwise False.print(str.endswith(suffix))print(str.endswith(suffix, 20))suffix = "is"# 2 indicates the starting position in the string# 4 indicates the ending position in the stringprint(str.endswith(suffix,2,4))print(str.endswith(suffix,2,6))

8. String Formatting

str = "Website name: {name}, Address {url}"# Format the characters in the above string{} to the desired charactersprint(str.format(name="Baidu", url="www.baidu.com"))

9. Change the string encoding format

str = "this is string example....wow!!!"# Change the string encoding formatprint(str.encode('utf-8'))

10. Join elements of a sequence into a new string with a specified character

str = "-"seq = ("a", "b", "c") # Elements in the string sequence must be strings# Join the elements in the seq sequence with the str string to generate a new string.print(str.join(seq))

11. Convert between strings and binary

data = '王佃超'# Convert the string to binarynew_data = data.encode('utf-8')# Convert the binary encoding back to stringyhf = new_data.decode('utf-8')print(yhf)

Comprehensive Summary of Common Python String Methods

Long press or scan the QR code below to get free Python public courses and hundreds of GB of learning materials organized by experts, including but not limited to Python e-books, tutorials, project orders, source code, etc.

▲ Scan the QR code - Get it for free
Recommended Reading
【Image】12 Python One-Liners You Must Know
10 Useful Python Libraries You Can Use Like a Pro
Ten Thousand Words Practical Case Tutorial! Python+SQL JD User Behavior Analysis, Save for Practice!
5 Python Libraries Sharing | Full of Dry Goods

Click Read the original text to learn more

Leave a Comment