Six, python basis: common operating string and built-in method

Six, python basis: common operating string and built-in method

1. What string? How the assignment? :

In fact, the string in single quotes in the English input method or double quotes string of characters, which can write numbers, letters, common symbols, spaces will do, as long as enclosed by single quotes (or double), which is the string .

s = '这是一串字符串'
s = '1111'
s = 'abcd'
s = ''
s = 'abs,de .de [] 。'

Single or double quotes can be. But if the string which also need to use quotation marks, then you look at it, but the outer layer is a single quote or double quotes, single quotes if the outer layer is used, then the inner layer to use double quotation marks, and vice versa.

If you have used the same inner and outer layers of quotation marks, then the program will get an error: invalid syntax, invalid syntax
Here Insert Picture Description

2, the conventional method for operating string are: Index, slicing, formatting method, the type of conversion:

2.1, the index string related operations: (2 cases)

2.1.1, obtain the character string for a location by index value:

String is ordered, that is, it has an index value for each character, which is corresponding to the position in the string.
Precautions:
index value starting from 0;
each space will occupy a position, it also has a corresponding index value;
for example as follows:
Here Insert Picture Description

2.1.2, obtain the index value of the character in the string of characters by:

For a string s, if we want to know the characters 'h' at which position inside it, using the index method
Here Insert Picture Description
, but there are 3 l, if we use the index ( 'l'), what will it return?
It will return to the search of a 'l' index value

We do not want to know that the first 'l' index value, just want a second 'l' index value of how to do?
index (value, start, end) method can be passed three parameters, the first one is the value (required), the second is the start position, the third end position.
Usage is as follows:

s.index('l')            # 获取第一个l在字符串中的索引值,输出是2
s.index('l', 3)         # 从字符串的索引值3开始,获取之后的第一个l的索引值
s.index('l', 4)         # 从字符串的索引值4开始,获取之后的第一个l的索引值
s.index('l', 3, 5)      # 从字符串的索引值35之间部分中,获取第一个l的索引值

Output:

2.2, string sections:

usage:

s = 'hello, world'

s[0]            # 索引取值:取出字符串中索引值为0的字符(对应就是第1个字符)
s[3]            # 索引取值:取出字符串中索引值为3的字符(对应就是第4个字符)
s[1:4]          # 切片取值:取出字符串中索引值1-4的字符
s[0: -1]        # 切片取值:取出字符串中索引值0到最后一个的字符
s[:-2]          # 切片取值:取出字符串中索引值0到倒数第2个字符
s[:]            # 切片取值:取出该字符串的所有字符

Output Results:
Here Insert Picture Description
Note:
Python slice values in both strings or lists for the slice value, are care regardless tail, such as the above s [0: -1], although it is set to 0 to the last character, but in the end a character is not to cut.

2.3, formatting methods:

Generally the format or% s (), see an article, formatted placeholder

2.4, type conversion:

Generally is used to turn the string or floating-point number into an integer, it may be of value arithmetic operation.
Here Insert Picture Description

3, and its role string common methods:

Do not make specific presentations, try it for yourself what the result output

s = 'hello, world'
str = 'hello, world'

len(s)                          #返回字符串的长度
str(s)                          #将x转换为字符串
chr(s)                          #返回unicode编码x对应的字符串
ord(s)                          #返回字符串x对应的unicode编码
hex(s)                          #返回整数x对应的十六进制数
oct(s)                          #返回整数x对应的八进制数
str.lower()                     #返回字符串的副本,全部字符小写
str.upper()                     #返回字符串的副本,全部大写
str.split(sep = None)           #返回一个列表,sep是分隔符
str.count(sub)                  #计算子字符串sub出现的次数
str.replace(old,new)            #用new字符串代替old字符串
str.center(width,fillchar)      #使字符串居中显示,fillchar是可选参数,可填充中文空格
str.strip(chars)                #去掉char左右两侧的字符
str.join(iter)                  #将iter变量除最后元素外每个元素后增加一个str字符串
str.capitalize()                #将字符串的第一个字母变成大写,其他字母变小写
str.isdigit()                   #检测字符串是否只由数字组成

4, calculation and determination of the string:

4.1, string concatenation: using the plus '+' can be stitched together a plurality of strings

Here Insert Picture Description
Here Insert Picture Description
Note: If the string is not spliced, but rather an integer or other type, being given (and this is a difference java and javascript which even integer and string concatenation, the program will automatically turn into a string of integers, then splicing )
Here Insert Picture Description

Copy 4.2, strings: use multiplication sign *

Here Insert Picture Description

4.3, when I want to whether a character in a string, how?

Used in the determination, if there is a string s, then the determination will be obtained in using a True, otherwise give False
use not in the determination, the result is the opposite.
Here Insert Picture Description

Published 47 original articles · won praise 74 · views 7895

Guess you like

Origin blog.csdn.net/Jacky_kplin/article/details/104779307