19-String related, slicing and str commonly used methods in python

1. Recognize strings

Strings are the most commonly used data type in Python. We generally use quotation marks to create strings. Creating a string is easy, just
assign a value to the variable.

a = 'hello world'
b = "abcdefg"
print(type(a))
print(type(b))

Note: The console display result is <class 'str'>, that is , the data type is str (string).

1 String characteristics

1. A pair of quoted strings

name1 = 'Tom'
name2 = "Rose"

2. Three-quoted string

name3 = ''' Tom '''
name4 = """ Rose """
a = ''' i am Tom,
nice to meet you! '''
b = """ i am Rose,
nice to meet you! """

Note: Strings in the form of triple quotation marks support line breaks.

2. Thinking: How to create a string I'm Tom?

c = "I'm Tom"
d = 'I\'m Tom'


print(c)
print(d)

2. String slicing

Slicing refers to the operation of intercepting a part of the object of the operation. Strings, lists, and tuples all support slicing operations.

1. The slicing syntax

Sequence [start position index: end position index: step length]

  1. Does not include the data corresponding to the end position subscript, both positive and negative integers are acceptable;
  2. The step length is the selection interval, which can be positive or negative integers. The default step length is 1.

2. Practical examples

1. Positive number

Do not write start, the default start from 0
Do not write end, which means the selection to the end The
default step size is 1

name = "abcdefg"
print(name[2:5:1]) # cde
print(name[2:5]) # cde
print(name[:5]) # abcde
print(name[1:]) # bcdefg
print(name[:]) # abcdefg
print(name[::2]) # aceg

2. Negative numbers

name = "abcdefg"

for i in range(-1, -8, -1):
    print(f"name[{i}]: {name[i]}")

Output:

name[-1]: g
name[-2]: f
name[-3]: e
name[-4]: d
name[-5]: c
name[-6]: b
name[-7]: a

When the step size is negative, it means to select from the right to the left, and select backwards

name = "abcdefg"

#print(name[::-1]) #gfedcba, 步长是负数,表示倒序
print(name[-4:-1:]) # def

print(name[-4:-1:-1]) # 这个是选不到任何东西的
#因为 -4 —— -1 方向是从左到右选取,而 -1 代表右侧往左侧选,倒着选 两个方向相反,选不到任何东西

3. If the selected direction (the direction from the start to the end of the subscript) conflicts with the direction of the step length, the data cannot be selected

3. String commonly used methods

2. Modification

The so-called modifying a string refers to modifying the data in the string in the form of a function.

1. replace(): replace

1. Grammar

字符串串序列列.replace(旧⼦子串串, 新⼦子串串, 替换次数)

Note: If the number of replacements exceeds the number of occurrences of the substring, the number of replacements is the number of occurrences of the substring.

2. Quick experience

mystr = "hello world and itcast and itheima and Python"

# 结果: hello world he itcast he itheima he Python
print(mystr.replace('and', 'he'))

# 结果: hello world he itcast he itheima he Python
print(mystr.replace('and', 'he', 10))

#hello world he itcast he itheima and Python
print(mystr.replace('and', 'he', 2))

# 结果: hello world and itcast and itheima and Python
print(mystr)

Note: According to whether the data can be directly modified, it is divided into two types: variable type and immutable type. When the data of the string type is modified, the original string cannot be changed. The type that cannot directly modify the data is the immutable type.

2. Split(): Split the string according to the specified characters.

1. Grammar

split returns a split list

字符串串序列列.split(分割字符, num)

Note: num represents the number of occurrences of the split character, and the number of data to be returned will be num+1.

2. Quick experience

mystr = "hello world and itcast and itheima and Python"


# 结果: ['hello world ', ' itcast ', ' itheima ', ' Python']
print(mystr.split('and'))


# 结果: ['hello world ', ' itcast ', ' itheima and Python']
print(mystr.split('and', 2))


# 结果: ['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
print(mystr.split(' '))


# 结果: ['hello', 'world', 'and itcast and itheima and Python']
print(mystr.split(' ', 2))

Note: If the split character is a substring in the original string, the substring will be lost after splitting.

3. join(): Use one character or substring to combine string strings, that is, combine multiple string strings into a new string string.

1. Grammar

字符或⼦子串串.join(多字符串串组成的序列列)

2. Quick experience

list1 = ['chuan', 'zhi', 'bo', 'ke']
t1 = ('aa', 'b', 'cc', 'ddd')

# 结果: chuan_zhi_bo_ke
print('_'.join(list1))

# 结果: aa...b...cc...ddd
print('...'.join(t1))

Guess you like

Origin blog.csdn.net/sgy1993/article/details/114971411