Create a character
var1 = 'Hello World! '
var2 = "Runoob"
Copy the code
Python string operators
String concatenation
* repeats the string
STR [0:2] does not contain the third character.
In member operator – Returns True if the string contains the given character
Not in member operator – Returns True if the string does not contain the given character
% format string
a = "Hello"
b = "Python"
print("A + b output result:", a + b) # print("A * 2 output result:", a * 2)
print("A [1] Output result:, a[1])
print("A [1:4] Output result:", a[1:4])
if "H" in a:
print("H is in variable A.")
else:
print("H is not in variable A.")
if ("M" not in a):
print("M is not in variable A.")
else:
print("M is in variable A.")
print(r'\n')
print(R'\n
Copy the code
Python String Formatting
String formatting in Python uses the same syntax as the sprintf function in C.
print ("My name is % S and I am % D!" % ('Ming'.10It's like changing the ", "in c to" % ".Copy the code
Python built-in string functions (part)
Capitalize () method
Make the first letter of the string uppercase and the other letters smaller. str.capitalize()
The count () method
The count() method counts the number of occurrences of a character in a string. Optional arguments are the start and end positions of the string search. str.count(sub, start= 0,end=len(string))
The find () method
The find() method checks if the string contains the substring STR. If beg and end are specified, it checks if they are in the specified range. If beg and end are specified, it returns the starting position of the index in the string. If no index value is included, -1 is returned. str.find(str, beg=0, end=len(string))
The join () method
Use to concatenate the elements in a sequence to generate a new string. str.join(sequence)
s1 = "-"
seq = ("r"."u"."n"."o"."o"."b"Print (s1.join(seq))Copy the code
Len () method
The len() method returns the length or number of items of the object (characters, lists, tuples, and so on). Len (s) returns the length of the object.
The lower () method
Converts all uppercase characters in a string to lowercase. str.lower()
The upper () method
Converts lowercase letters in a string to uppercase letters. str.upper()