A python string is a string of characters made up of numbers, letters, and underscores, and we can use quotation marks to create strings. Such as:
str = ' Helloworld '
There is no char type in Python, and a single character is also used as a string;
The list of strings in Python has 2 order of values:
A. From left to right, the default index starts at 0 and the index length is the longest of the string length-1
B. From right to left, default index starts from-1
Python accesses substrings, you can use square brackets to intercept strings, such as:
>>> str = ' Helloworld '
>>> Str[0]
H
#左边的开始是包含了下边界. The following result contains the value E of str[1], and the maximum range taken is not including the upper boundary, or str[5]
The Value O
>>> Str[1:5]
' Ello '
>>>
Print str # output entire string
Print Str[0] # The first character in the output string
Print Str[1:5] # The string between the second and the fifth in the output string
Print Str[1:7:2] # Every step of the output string A string between the second and seventh
Print Str[2:] # output A string starting from the third character
Print str*2 # output String two times
Print str+ ' test ' # output concatenated string
------------------------------
Characters commonly used in Python:
\ #用到特殊符号时需使用转义字符
+ #字符串连接
* #连续输出多个同一字符 such as str*2
[:] #输出字符串的一部分
In #若字符在字符串中则返回True
Not in #若字符不在字符串中则返回True
------------------------------
Commonly used string built-in functions:
>>> str.count (' l ') #统计str字符串中字符 ' l ' occurrences
3
>>> str.find (' o ') #计算str字符串中字符 ' o ' where the index appears
4
>>> str.index (' o ') #计算str字符串中字符 ' o ' where the index appears
4
>>> str.replace (' l ', ' 2 ') #把str字符串中的字符 ' l ' replaced with ' 2 '
' He22owor2d '
------------------------------
Python Base 4-string