#!/usr/local/bin/python3 # -*- coding:utf-8 -*- name="my wife is mahongyan" #----------Capitalized---------- #print(name.capitalize()) #----------Character Statistics---------- #print(name.count('m')) #----------Character Beautiful Printing---------- #print(name.center(50,'-')) #Print a total of 50 characters, if not enough, use '-' on both sides of the string to make up #---------- Determine what the string ends with ---------- #print(name.endswith('yan')) #Determine whether the string ends with 'yan', if it is, return True #----------tab key conversion---------- #name1="my \twife is mahongyan" #print(name1.expandtabs(tabsize=30)) #Convert the tab key in the string to a specified number of spaces #----------Remove subscript---------- '''print(name.find('wife')) #Get the subscript of the first character in the string 'wife' in the string #Note: 1. Counting from 0, a space bar is also counted as one # 2. If the string contains two 'wife', return the subscript value in the first 'wife' print(name[name.find('wife'):7]) #Slice operation, start slicing from the subscript where 'w' is located, until 'e', that is, take out "wife"''' #----------format---------- '''name2="my wife is {name} and she is {age} years old" print(name2.format(name='mahongyan',age=26)) print(name2.format_map({'name':'mahongyan','age':26})) #The parameter is the dictionary format''' #----------Determine whether the string is pure Arabic numerals + characters------------ #print(name.isalnum()) #Note: It is 'pure' Arabic numbers and characters, if there are spaces or other special characters in the string, it will return False #----------Determine whether the string is a pure English character---------- #print(name.isalpha()) #If the string contains numbers or other non-English characters, return False #----------Determine whether the string is a decimal number------------ '''name3="0100" print(name3.isdecimal()) #Note: If the string is a binary number, it also returns True''' #----------Determine whether the string is a valid identifier (variable name)---------- '''name4="_1x" print (name4.isidentifier ()) '' ' #----------Determine whether the string is an integer type---------- '''name5="22" print(name5.isdigit())''' #----------Determine if the string is a lowercase letter------------ #print(name.islower()) #The string can contain numbers or special characters, that is, only determine whether the letters contained in the string are lowercase #----------Determine if the string is an uppercase letter---------- #print(name.isupper()) #The string can contain numbers or special characters, that is, only determine whether the letters contained in the string are uppercase #----------Determine if string is a 'pure' number---------- #print(name.isnumeric()) #There is no decimal point, it is not much different from isdigit #----------Determine if the string is a space------------ '''name6=' ' print(name6.isspace()) #One to n spaces all return True''' #----------Determine whether the string is in title format------------ '''name7='My Wife Is Mahongyan' print(name7.istitle()) #That is, the first letter of each word is capital ''' #---------- Convert string to title format ---------- #print(name.title()) #----------join usage---------- #print('*'.join(['a','b','c'])) #Not yet understood #----------The end of the string is filled and printed ---------- #print(name.ljust(50,"-")) #Make the length of the output string 50, if the length of the string is not enough, pad '-' at the end #----------The header of the string is filled and printed ---------- #print(name.rjust(50,"-")) #Make the length of the output string 50, if the length of the string is not enough, fill in the header with '-' #----------Convert letters in string to uppercase/lowercase---------- '''print(name.upper()) #Only convert all letters print(name.lower())''' #----------Remove spaces or carriage returns from the left and right sides of the string---------- '''print(" \nabc".lstrip()) #Remove the left side print("abc \n".rstrip()) #Remove the right side print(" \nabc \n".strip())#Remove both sides ''' #----------Encryption 1---------- '''p=str.maketrans("abcdefg","1234567") #将"abcdefg"对应成"!@#$%^&" print("bad".translate(p)) #Pass the password rule p into the translate function, and then convert "bad" into the encrypted string ''' #----------Encryption 2---------- #print(name.translate(str.maketrans("abcdefg","1234567"))) #The effect is the same as 1 #----------replace---------- '''print(name.replace('a','A')) #Replace all characters 'a' in the string with 'A' print(name.replace('a','A',2)) #Replace the first two 'a' in the string with 'A' ''' #----------search---------- #print(name.rfind('y')) # Traverse the string from left to right and return the largest subscript of the character 'y' in the string #----------Conversion list---------- '''print(name.split()) #Split the string by spaces and put it into a list in turn print("1+2+3+4".split('+'))#Split the string according to the character 'y' and put it into a list in turn, but the character 'y' disappears print("1\n2\n3\n4".splitlines())#Split the string according to the newline and put it into a list ''' #----------Find---------- '''print(name.startswith('my')) #Find if the string 'my' exists in the string, if it exists, return True print(name.startswith('my',0,4)) #Search for the existence of the string 'my' in the subscript 0~4 of the string, if it exists, return True''' #----------Letter case conversion---------- #print(name.swapcase()) #Convert uppercase letters in the string to lowercase, and lowercase letters to uppercase #----------Hexadecimal padding---------- #print(name.zfill(30)) #Specify the length of the string as 30. If the length of the string is not enough, fill 0 at the beginning of the string; usually used for hexadecimal conversion