[python learning] basic string operations-5

Strings can be set to null characters, such as:

str_1=""
print(str_1)

String variable name [subscript], a value in the query string

Note: 1. If you need to start from the positive sequence, you need to start from 0

        2. If you need to start from the reverse order, you need to start from -1

        2. If the subscript does not exist, an error will be reported

#正序取值
str_1="自动化测试正式开始"
print(str_1[0])
print(str_1[1])
print(str_1[2])
print(str_1[3])
print(str_1[4])


----------------------打印结果----------------------

自
动
化
测
试

#倒序取值
str_1="自动化测试正式开始"
print(str_1[-1])
print(str_1[-2])
print(str_1[-3])


--------------------打印结果--------------------

始
开
式

To get the length of the string, use: len()

Note: The default starts from "1", 1,2,3...

#len()
str_1="我是程序员张三,现在此刻正在加班"
print(len(str_1))


---------------------打印结果---------------------

16

slice: string variable name [start coordinate: end point coordinate: step size]

Note: 1. The default start index is 0, and the default step size is 1

        2. Take the head but not the tail

        3. If the step size is positive, it means positive sequence; if the step size is negative, it means reverse sequence

#正序
str_1="我是程序员张三,现在此刻正在加班"
print(str_1[0:7:1])
print(str_1[0:10:2])


--------------------打印结果--------------------

我是程序员张三
我程员三现
#倒序
str_1="我是程序员张三,现在此刻正在加班"
print(str_1[::-1])



------------------------打印结果------------------------

班加在正刻此在现,三张员序程是我

Guess you like

Origin blog.csdn.net/admins_/article/details/122282202