String operations - Python base

String

Refers to the string of " "double quotes, or ' 'any text in single quotes, for example, "abaa", '1232' is usually a string in quotation marks.

String variable assignment

>>> str='123'   # 使用单引号定义字符串
>>> str    		# 输出变量的值, 单引号括起来的是字符类型
'123'

>>> str="123"	#使用双引号定义字符串
>>> str			# 输出变量的值, 单引号括起来的是字符类型
'123'	

Clear variable values ​​- del

Want to clear the values ​​of variables are given:

>>> str='武汉'     	# 变量赋值字符串
>>> str				# 输出变量的值, 单引号括起来的是字符类型
'武汉'


>>> del str			# 清除变量的值
>>> str
<class 'str'>		# 输出变量的值, 无返回结果

String escape - \

Want to output single or double quotation marks in the string, it is necessary to use \escape

>>> s='Let\'s go'
>>> str
"Let's go"
>>> 

View data types - type

Use typecan view the data type of a variable

>>> str='Let\'s go'
>>> str
"Let's go"

>>> type(str)
<class 'str'>   	#str 字符串类型

>>> a=1
>>> type(a)
<class 'int'>   	# int 整数类型

Set string format: Lite

% s for the conversion specifier , pointed out that the talk about values into what

>>> v1='武汉 %s, 武汉 %s !!'
>>> v2=('必胜', '加油')
>>> v1 % v2
'武汉 必胜, 武汉 加油 !!'

Naming replacement character

>>> "Dear {name} , Let\'s play {game}".format(name='Dongdong' , game='LEGO')
"Dear Dongdong , Let's play LEGO"

Replacement character has no name, the name can be used as an index

>>> '{} , {}  and {}'.format('Dongdong' , 'tom' , 'Jiang')
'Dongdong , tom  and Jiang'

>>> '{0} , {2} and {1}'.format('Dongdong' , 'tom' , 'Jiang')
'Dongdong , Jiang and tom'

For the character of the operation

String sections

  • Is a string iterable, that is to say each has a corresponding string is actually an index value
  • We will be assigned to a string variable, in fact, each character has its own location number, starting from 0.
    string[开始位置:终止位置:步长和方向]
>>> str='python'
>>> str[0]
'p'
>>> str[1]
'y'
>>> str[2]
't'
>>> str[3]
'h'
>>> str[4]
'o'
>>> str[5]
'n'
>>> str[6]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
  • You can also reverse, starting from -1
>>> str
'python'
>>> str[-1]
'n'

Note: Select the closed section belonging to the left and right open type, that is, from the start "start" position to the "end" before the end of a bit (do not include the end of the bit itself).

Here an example to explain the string, in fact, the list will have the same approach.

>>> str
'python'

>>> str[2:4]	#从第二位开始,取第四位之前(不含)的所有字符.
'th'
  • Step: in a piece of content in accordance with the contents specified step length robbing
>>> str[::2]   # 起始和结束未知未指定,默认从头到尾, 取偶数位

You can also take backwards

>>> str[::-2]
'nhy'

Find string contents - find

Str is included in the detection mystr, if the index value of the return, otherwise -1
str.find(str, start=0, end=len(mystr))

>>> str='hello, python'		# 重新定义变量str

>>> str.find('o')   		# 在str中从头查找, 字母o的位置(第一位是0)
4
>>> str.find('o',2) 		# 在str中的第二位开始查找, 字母o的位置(第一位是0)
4
>>> str.find('o',5)			# 在str中的第五位开始查找, 字母o的位置, 那么跳过了第一个o, 找到了第二个o所在的位置
11

Find a word, then the output is the position of the first character to find the words, we know the word python to a total of 6 characters

>>> str.find('python')
7
>>> str[7:7+6+1] 	# 为什么是7+6+1 , 7-是从第七位之后向后找6位, 由于是左开右闭, 那么还要再多加1位
'python'

Statistics string, number of characters that appear - count

Returns the number of times between the start and end str appear in mystr inside

str.count(str, start=0, end=len(mystr))

>>> str 			# 变量定义字符串为 hello, python
'hello, python'

>>> str.count('o') 	# 变量中字母'o', 出现了几次.
2
>>> str.count('o',0,5) # 变量中第0个字符到第5个字符, 之间'o'出现了几次
1
>>> str.count('pyth')  # 变量中'pyth'字符串, 出现过几次
1

Replace - replace

Replace mystr in str1 into str2, if the count is specified, the replacement does not exceed count times.

mystr.replace(str1, str2, mystr.count(str1))

Replace, does not change the value of the original variable

>>> str
'hello, python'

>>> str.replace('l','加油')    # 找到str中所有的字母l, 替换为'加油'
'he加油加油o, python'
 
>>> str.replace('o','武汉',1)   # 找到str中第一个的字母o, 替换为'加油'
'hell武汉, python'

Remove the character ending spaces - strip

>>> str='Hello , python    '
>>> str
'Hello , python    '
 
>>> str.strip()
'Hello , python' 

Separator sections - split

A string of continuous string of the specified character, separated
str.split(str=" ", 2)

>>> str
'hello, python'
>>> 
>>> str.split('')		# 字母o 作为分隔符
['hell', ', pyth', 'n']
>>> 
>>> str				# 变量原值不受影响
'hello, python'

Separator sections - partition

A string of continuous string, in accordance with the string of characters separated
str.split(str=" ", 2)

>>> str
'hello, python'

>>> str.partition('lo')
('hel', 'lo', ', python')

Align - center

>>> str.center(30)		# 一共30个字符,其中字符串居中对齐显示
'        hello, python         '
>>> 
>>> str.center(30,'-')		# 一共30个字符,其中字符串居中对齐显示, 两边填充指定字符
'--------hello, python---------'
>>> 

Change case - lower & upper & title

>>> str.upper()		# 转换为大写
'HELLO, PYTHON'

>>> str.title()		# 首字母大写
'Hello, Python'
Published 23 original articles · won praise 0 · Views 4481

Guess you like

Origin blog.csdn.net/strawberry1019/article/details/104532218