string

Strings are basic data types, immutable sequences of characters

The residency mechanism for strings

S1 = "Python" s2 = "Python" s3 = "Py" + "thon" print(s1, id(s1)) Print (s2, id(s2)) print(s3, id(s3)) print(s3, id(s3)) Instead of running the integer numbers between 4-[-5,256], the sys.Intern () method forces the contents of the same string to point to the same object. PyCharm optimizes the string; A string that does not conform to an identifier, and an integer number that is not between [-5,256], will reside as long as the content is consistent.Copy the code

String query

Print (s1. Index ("lo")) # 3; ValueError: substring not found print(s1.find("lo")) # 3; -1 print(s1.rindex("lo")) # 9; ValueError: Substring not found print(s1.rfind("lo")) # 9; -1 # print(s1.index("python")) # ValueError: substring not found print(s1.find("python")) # -1; -1 # print(s1.rindex("python")) # ValueError: substring not found print(s1.rfind("python")) # -1; Finds the last occurrence of a substring, returns -1 if no substring existsCopy the code

String case conversion

name = "python,HI" print(name.upper()) # PYTHON,HI; Print (name.lower()) # python,hi; Print (name.swapcase()) # PYTHON,hi; Print (name. Capitalize ()) # Python,hi; Print (name.title()) # Python,Hi; Upper_name = name.upper() print(id(name)) print(id(upper_name))Copy the code

String alignment

Print (name.center(2)) # python; Print (name.center(10)) print(name.center(10, "*")) # **python**; Print (name.ljust(10)) print(name.ljust(10, "*")) # python****; Print (name.rjust(10)) print(name.rjust(10, "*")) ****python; Specifies that the padding is * # right justified; Print (name.zfill(10)) # 0000Python; Print ("-2021". Zfill (10)) # -000002021; The 0 of a negative string is appended to the minus signCopy the code

String separation

Name = "python hello hi" print(name.split()) # ['python', 'hello', 'hi']; Name = "python-hello-hi" print(name.split("-")) # ['python', 'hello', 'hi']; Print (name.split("-", maxsplit=1)) # ['python', 'hello-hi']; Name = "python-hello-hi" print(name.rsplit("-")) # ['python', 'hello', 'hi'] print(name.rsplit("-", maxsplit=1)) # ['python-hello', 'hi']Copy the code

String judgment

# is a valid identifier; Name = "python,hello" print(name.isIdentifier ()) # False name = "hello_sam_123" Print (name.isidentifier()) # True # Print (name.isspace()) # True # Print (name.isspace()) # True Print (name.isalpha()) # True name = "python_hello" print(name.isalpha()) # False # Check whether the string consists of decimal digits name = "Python" print(name.isdecimal()) # False name = "123456789" print(name.isdecimal()) # True name = "123456789" Print (name.isdecimal()) # False # Name = "python" print(name.isnumeric()) # False name = "123" print(name.isnumeric()) # True; Name = "python_123" print(name.isalnum()) # False name = "python123" print(name.isalnum()) # TrueCopy the code

String substitution

# replacement string name = "hello, python, python, python" print (name) the replace (" python ", "Java")) # hello, Java, Java, Java replacement # set the maximum number of times: Print (name. Replace ("python", "Java ", 2)) # hello, Java, Java,pythonCopy the code

String merging

Name = [" Java ", "python", "hello"] print("-".join(name)) # java-python-hello name = (" Java ", "python", Name = "python" print("*".join(name)) # P*y*t*h*o*nCopy the code

String comparison

Print ("a" > "b") # False print(ord("a"), ord("b")) # 97 98 print(CHR (97), chr(98)) # a bCopy the code

Slicing of strings

# string is an immutable type and cannot be added, deleted, or modified. Name1 = name[:5] name2 = name[6:] print(name1) # hello print(name2) # Python name1 = name[0:5] name2 = name[6:] print(name1) # hello print(name2) # Python name1 = name[1:5:2] print(name1) # el name1 = Name [::2] # start from 0 until the last character; Print (name1) # hloPto name1 = name[::-1] print(name1) # nohtyP,ollehCopy the code

Formatted string

# % placeholder name = "Python" age = 20 print (" my name is % s % d this year "% (name, Name = "Python" age = 20 print(" I am {0} this year {1}". Format (name, Age)) # f-string name = "Python" age = 20 print(f" I call {name} this year {age}") # Use % print # (" % d % 99) [99] print (# 10 "% d" % 99) [99]. Print ("%f" % 3.141592) # 3.141592 print("%.3f" % 3.141592) # 3.142; Print ("%10.3f" % 3.141592) # [3.142]; Both set width and preserve decimal # string numeric width and precision; Use {} print (" {0} ". The format (3.141592)) print (" {0:3} ". The format (3.141592)) # 3.14. Print ("{0:.3f}". Format (3.141592)) # 3.142; To 3 decimal places print (" {0:10. 3 f} ". The format (3.141592)) # [3.142]; Set the width and preserve the decimal placeCopy the code