Python data container - str (string)

1. String is also a member of the data container

Strings are also a member of data containers.
A string is a container of characters, and a string can store any number of characters.
Like other containers such as lists and tuples, strings can also be accessed through subscripts.

All subsequent operations use my_str as an example:

my_str = "HelloWorld the world is nice"

2. String operations

Strings are also unmodifiable data containers. Modifying the characters of a specified subscript, removing characters of a specific subscript, appending characters, etc. cannot be completed. If you perform the above operation, you will get a new string, and the old one cannot be modified. , the following operations will generate a new string!

2.1. Obtain the character at a specific position and find the subscript of a specific element through subscripts.

# 通过下标获得特定位置的字符
value = my_str[2]
value2 = my_str[-6]

print(f"my_str字符串中下标为2的元素的值是{value},
 my_str字符串中下标为-6的元素的值是{value2}")
# 输出:my_str字符串中下标为2的元素的值是l, my_str字符串中下标为-6的元素的值是s

2.2. Index method: The usage method is the same as list and tuple.

# 查找特定元素的下标
value3 = my_str.index("the")
print(f"在字符串{my_str}中查找the,它的起始下标是:{value3}")
# 输出:在字符串HelloWorld the world is nice中查找the,它的起始下标是:11

2.3. Replacement of strings (this is new knowledge, not found in lists and tuples)

Syntax: String.replace(String 1, String 2)
Function: Replace everything in the string: String 1 with String 2 (delete all the original things in String 1 and replace them with String 2 thing).
Notice:

Instead of modifying the string itself, you get a new string. The new string is to be received by the new string name.

# 替换repalce
New_my_str = my_str.replace("World","世界") 
# 原来所有的World都会被世界两个字替代

print(New_my_str)
# 输出:Hello世界 the 世界 is nice
# 一定要注意,是用新的字符串来接收的,原来字符串并没有被修改。

2.4. String splitting (this is new knowledge, not found in lists and tuples)

Syntax: String.split (separator string)
Function: Divide the string into multiple strings according to the specified delimiter string, and store them in the list object.
Note: The string itself does not change, but gets a list object

my_str_list = my_str.split(" ")   #  按照空格分隔
print(f"分割后的字符串变成列表my_str_list: {my_str_list}")
# 输出:分割后的字符串变成列表my_str_list: ['HelloWorld', 'the', 'World', 'is', 'nice']

2.5 Regularization of strings (remove leading and trailing spaces or remove leading and trailing specified strings)

grammar:

String.strip() (remove leading and trailing spaces)
string.strip (specified string) (remove specified leading and trailing spaces)


my_str = "   12HelloWorld the World is nice21"
value1 = my_str[2]
value2 = my_str[-6]

my_str1 = my_str.strip()
my_str2 = my_str.strip('World')
my_str3 = my_str.strip('12')
my_str4 = my_str1.strip('12')

print(f"去除前面的空格后是: {my_str1}")  
# 输出结果:去除前面的空格后是: 12HelloWorld the World is nice21

print(f"去除字符串World后是: {my_str2}")
# 输出结果:去除字符串World后是:    12HelloWorld the World is nice21

print(f"去除字符串12后是: {my_str3}")
# 输出结果:去除字符串12后是:    12HelloWorld the World is nice

print(f"在去除空格的基础上去除字符串12后是: {my_str4}")
# 输出结果:在去除空格的基础上去除字符串12后是: HelloWorld the World is nice

Conclusion: It is only valid for the front or end of the string. If the string to be removed is in the middle, it is invalid.

And the string containing the characters in the specified string is removed, that is, the specified string is "12", the first and last "12" and "21" containing "1" and "2" will be removed.

2.6 Count the number of occurrences of a certain character in a string

Syntax: string.count(a certain character)

The result is an integer, which is the number of times a certain character appears.

2.7 Count the length of strings

Syntax: len (string)

String operations Features Example

Get the character at a specific position by subscripting

and find the subscript
index method of a specific element

The usage method is the same as list and tuple. value = my_str[2]
value2 = my_str[-6]
value3 = my_str.index("the")

String replacement

String.replace(old string, new string)

The original string remains unchanged and a new string is generated.

The new string is to be received by the new string name.

New_my_str = my_str.replace("World","世界")
String split split
string.split (separator string)
Splitting strings usually uses spaces as delimiters my_str_list = my_str.split(" ") # Separate by spaces
 

String regularization operations
String.strip() (remove leading and trailing spaces)
String.strip (specified string) (remove specified leading and trailing spaces)

Only valid for the front and back my_str3 = my_str.strip('12')

Count the length of a string

Syntax: len (string)

Count the number of occurrences of a character in a string

Syntax: string.count(a certain character)

Guess you like

Origin blog.csdn.net/weixin_48060069/article/details/132266774