Python3 string common method

Advanced string of use

Formatted output string

PS: string% s; integer% d;% f float

name = '张三'
age = 18


strs = "我是%s,我今年%d岁了" %(name,age)
print(strs)

See above code, there are a string and% s% d, which is to tell the Python string inside the values ​​of these two things need to be read from the back

Therefore, there is behind% (name, age), he is sequentially transmitted to the front of the character and the% d% s

Of course, in addition to the above-described methods, you can also use the format method

strs = "我是{name},我今年{age}岁了".format(name='李四',age=18)
format方法,在前面的字符串中使用{参数名},在format()中对该参数进行赋值即可

The second method uses format:

strs = "我是{0},我今年{1}岁了".format('张三',15)

Python strings and binary conversion

name = '张三'
bytename = name.encode('utf-8')
print(bytename)

name = b'\xe5\xbc\xa0\xe4\xb8\x89'.decode('utf-8')
print(name)

Capitalization

name = 'zhangsan'
print(name.capitalize())#首字母大写

Count the number of times a character appears

name = 'zhangsan'
print(name.count('a'))#统计字符a出现的次数

Output 50 characters, with less - instead of

name = 'zhangsan'
print(name.center(50,"-"))#输出50个字符,少了的用-代替

What is not so the judge to end

name = 'zhangsan'
print(name.endswith('an'))#判断以是不是以an结尾

Find character, returns its index to find, can not find or -1

name = 'zhangsan'
print(name.find('g'))# 查找g,找到返回其索引, 找不到返回-1

Judgment is not Arabic characters (letters and numbers)

name = 'zhangsan'
print(name.isalnum())# 判断是不是阿拉伯字符(字母与数字)

Judgment is not an integer

name = 'zhangsan'
print(name.isdigit())# 判断是不是个整数

Judgment is not a valid identifier (variable name)

name = 'zhangsan'
print(name.isidentifier())# 判断是不是个合法的标识符(变量名)

Judgment is not lowercase

name = 'zhangsan'
print(name.islower())# 判断是不是小写

Judgment is not capitalized

name = 'zhangsan'
print(name.isupper())# 判断是不是大写

Judgment is not only numeric characters

name = 'zhangsan'
print(name.isnumeric())# 判断是不是只有数字字符

Judgment is not the title (first letter capitalized)

name = 'zhangsan'
print(name.istitle())# 判断是不是标题(首字母大写)

A judgment is not something that can be printed

name = 'zhangsan'
print(name.isprintable())#判断是不是一个可以打印的东西

The strings added to the string sequentially in

name = 'zhangsan'
print(name.join(['1','2','3','4'])) #将字符串依次加入到后面字符串中

Length of the string 10, with less in the right place *

name = 'zhangsan'
print(name.ljust(10,'*'))# 字符串长度为10,少了的在右边用\*代替

Length of the string 10, less on the left, use *

name = 'zhangsan'
print(name.rjust(10,'*'))# 字符串长度为10,少了的在左边用*代替

Converted to lowercase

name = 'zhangsan'
print(name.lower())# 转换成小写

Uppercase

name = 'zhangsan'
print(name.upper())# 转换成大写

The removal of the left space or carriage return

name = 'zhangsan'
print(name.lstrip()) # 去除左边的空格或回车

Remove the right of the space or carriage return

name = 'zhangsan'
print(name.rstrip()) # 去除右边的空格或回车

White spaces on both sides or carriage return

name = 'zhangsan'
print(name.strip())  # 去除两边的空格或回车

Replace all characters

name = 'zhangsan'
print(name.replace('a',"A"))# 将a全部替换成A

Replace a character

name = 'zhangsan'
print(name.replace('a',"A",1))# 将a替换成A(替换一个即可)

Find the last character

name = 'zhangsan'
print(name.rfind('a'))# 找到最后一个a

The list is divided into a string

name = 'zhangsan'
print(name.split('a'))# 将字符串用a来进行分割成列表

The method of a futile

p = str.maketrans('abcde','12345')
# 把a的值定为1,b的值定为2,依次
print('qwera'.translate(p))# 把p传进去,e就等于5,e就等于2

Guess you like

Origin www.cnblogs.com/samtester/p/11721875.html