String manipulation

# Author :yanpengtao

name = "my name is ALEX"
print(name.capitalize()) # Capitalize the first letter
print(name.casefold()) # Convert all uppercase to lowercase
print(name.count("A")) # Count the number of occurrences of A
print(name.center(50, "-")) # output -----------------------ALEX------------- ------------
print(name.endswith("EX")) # Determine if the string ends with EX
print(name.expandtabs(tabsize=30)) # Convert \t to how long a space
print(name.find("name")) # Find name, find and return its index, and return -1 if not found
print(name[name.find("name"):]) # string slice

# Author :yanpengtao

name = "alex"

print(name.isalnum()) # Whether it is an Arabic number
print(name.isalpha()) # Is it a plain citation character
print(name.isdecimal())
print(name.isdigit()) # Is it an integer
print(name.isidentifier()) #Is it a valid identifier
print(name.islower()) # Is it lowercase?
print(name.isnumeric())
print("My Name Is".istitle()) #Each first letter is capitalized, is it title
print("My Name Is".isprintable()) #Is it printable
print("My Name Is".isupper()) #Whether it's all uppercase
print("|".join(["my", "name", "is", "alex"])) # Convert list to string
print(name.ljust(50, "*")) # Print according to 50 characters, if not enough, add * to the right
print(name.rjust(50, "*")) # Print according to 50 characters, if not enough, add * to the left
print("\nalex".lstrip()) #Remove newlines and spaces on the left
print("alex\n".rstrip()) #Remove newlines and spaces on the right
print("\nalex\n".strip()) #Remove newlines and spaces on both sides
p = str.maketrans("abcdef", "123456") # 123456 corresponds to abcdef
print("alex".translate(p)) # Then replace the abcdef contained in alex with 123456 respectively
print("alex li".replace("l", "L")) #替换
print("alex li".rfind("l")) # Find all l, return the subscript of the rightmost l
print("alex li".rsplit()) #Split the string into lists according to spaces
print("alex li".rsplit()) #Split the string into lists according to spaces
print("alex li li".rsplit("l")) #Split the string into lists according to l
print("alex\n li li".splitlines()) #Split the string into lists according to newlines
print("Alex Li".swapcase()) #Uppercase becomes lowercase, lowercase becomes uppercase

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325002032&siteId=291194637