Strings are the most commonly used data types in Python, and we can use single quotation marks (') or double quotation marks ("") to create strings.
A='hello'b="Hello"
All standard sequence operations such as (index, Shard, membership, length, minimum and maximum, etc.) are equally applicable to strings.
Formatting symbols commonly used in strings:
(%s formatted string)
Print ('hello,%s'%'World'# uses%s as a placeholder for ' world ' # Results Print (' also%s days '# uses%d as a placeholder for 10 # results # %s can not only format strings, but also format integers
(%d formatted integer)
Print (' %d days '# uses%d as a placeholder for 10 # results
Common methods of strings:
Find (): Used to detect whether a string contains substrings of STR, you can specify a range of start and end.
a= " hello, World " print (A.find (" wo " 6 # Returns the index of the matching value Span style= "COLOR: #0000ff" >print (A.find ( " kb " -1 # not found, Returns-1
print (A.find ( " wo , 3)" # Provide a starting point 6 # result print (A.find ( ' WD ", 6-1 # result
Print (A.find ('wo'# provides start and end # result print(A.find ( ' WD ', 3,7)# results
Lower (): Converts all uppercase characters in a string to lowercase
A='HeLlo'b=a.lower ()print# result
Upper (): Converts all lowercase characters in a string to uppercase
A='HeLlo'b=a.upper ()print# Results
Swapcase (): Convert all lowercase characters in a string to uppercase, lowercase characters
A='HeLlo'b=a.swapcase ()print# Result
Replace (): replaces the old string in a string with a new string
A='helloworld 'b=a.replace ('hello',' HELLO ' )print# results
Strip (): Removes the string from the Kinsoku specified character
A='++hello world++'b=a.strip ('+')Print(b) Hello world#Resultsb=a.strip ('++h')Print(b) Ello World#Resultsb=a.strip ('++d')Print(b) Hello Worl#Results
(self-hing ai) Python string