Operation of strings in Python (graphic details)

The following operations are all in pycharm


Pycharm official download address:

https://www.jetbrains.com/pycharm/download/#section=windows

Table of contents

The types of string operations in python are divided into the following types 

1. Formatting method

a.capitalize Change the first letter to uppercase

a.casefold all become lowercase and compare 

 a.center The string is filled with any symbol twice and centered

a.expandtabs modify the placeholder of the tab key

 a.ljust fills from the right of the string

a.rjust fills from the left side of the string #Consistent with ljust usage, different one fills on the left and one on the right

a.lower Change all uppercase characters in the string to lowercase

 a.swapcase case swap

 a.title is changed to title, that is, the first letter of each word is capitalized

 a.upper changed to uppercase

 a.zfill The original string is right-aligned, and the front is filled with 0

 a.strip removes the leftmost and rightmost spaces and extra characters in the string

 a.lstrip removes extra spaces and tab keys on the left a.rstrip removes extra spaces and tab keys on the right

 a.format references external variables

 Two, the string judgment method

 a.startswith determines what starts with the string #If there are spaces at the beginning and end of the string, the result is False

a.endswith Determine what ends in the string #If there is a space at the beginning and end of the string, the judgment result is False

a.isalnum Determines whether a string is a number

a.isalpha Determines whether the string is a letter

 a.isdigit judges whether the string is a number# can only judge integers

a.isnumeric What is the difference between judging whether a string is a number and isdigit?

a.isdecimal Returns True if the string contains only decimal characters, otherwise returns False.

a.islower Determine whether the string is lowercase

Is a.isupper all uppercase? Similar to the above

 a.isprintable Whether it can be printed

 a.isspace Is it a space 

a.istitle is title

 3. Check, modify, count, and replace strings

a.find Find a single character in the string starting from the left

 a.rfind is the same as find, the difference is that this is to find from the right

a.index is similar to the search method of find, the difference is that if find cannot find a value, it will return -1, while index will directly report an error 

 a.rindex # It is the same as rfind, start looking from the right, same as index 

a.count Counting can count the number of occurrences of a character in a string

 a.split splits all characters separated by spaces in the string into a single element by default

 a.rsplit is the same as split, but it starts cutting from the right and can also use maxsplit   

 a.splitlines cut with special symbols such as: \t \n .... etc.

a.removeprefix Remove certain words from the prefix

a.removesuffix The removal of a certain suffix has the same meaning as removeprefix, the difference is that it is removed from the last one

 a.replace replace

4. Special usage of strings 

a.join converts the list into a string and puts each element together in the specified format 

a.maketrans # Generate codebook 

a.translate # encryption  


The types of string operations in python are divided into the following types 

1. Formatting method

overview

a.capitalize  #首字符改成大写
a.casefold #为方便字符串之前对比,改成小写
a.center #字符串两遍填充
a.expandtabs #类似于tab键设定长度  这种写法在定义a的值时候需要加\t 如a = "abc\tabc"  \t为tab键默认占8个字符 这个可以修改\t的间隔
a.ljust #从字符串右边填充
a.rjust #从字符串左边填充
a.lower #全变小写
a.swapcase #大小写互换
a.title #改成标题,即每个单词首字母大写
a.upper #改成大写
a.zfill #字符串空的地方填0
a.strip #两边去死皮  就将字符串内最左边和最右边的空格和多余字符去掉
a.lstrip #左边去掉多余的空格与tab键
a.rstrip #右边去多余的空格与tab键
a.format #引用外部变量

Detailed explanation 

a.capitalize Change the first letter to uppercase

For example:

a = "this is my dog" #定义字符串    
print(a.capitalize()) #将首字母改成大写

operation result:

a.casefold all become lowercase and compare 

a = "this is my dog"
b = "This is My Dog"
print(b.casefold() == a) #表示将b的字符串全部小写并与a做对比

Running result:  #If you judge directly without adding casefold, the returned value is False

 a.center The string is filled with any symbol twice and centered

a = "this is my dog"
print(a.center(50,"-")) #表示字符串以“-”形式填充够50个字符,将a的内容居中

operation result:

a.expandtabs modify the placeholder of the tab key

a = "this is my\t dog"
print(a.expandtabs(10)) #字符串中\t代表 tab键 默认为8个占位符 括号内10代表将tab键改为10个占位符

operation result:

 a.ljust fills from the right of the string

a = "this is my dog"
print(a.ljust(30,"~")) #表示从字符串最右边填充到30个“~”符号

operation result:

a.rjust fills from the left side of the string #Consistent with ljust usage, different one fills on the left and one on the right

a.lower Change all uppercase characters in the string to lowercase

a = "THIS Is My Dog"
print(a.lower()) #将字符串中所有的大写变为小写,只能用于英文

 operation result:

 a.swapcase case swap

a = "THIS Is My Dog"
print(a.swapcase()) #将字符串中所有的大小写转换一下

operation result:

 a.title is changed to title, that is, the first letter of each word is capitalized

a = "THIS is my dog"
print(a.title()) #将字符串内容改成标题,并且首字母大写

operation result:

 

 a.upper changed to uppercase

a = "THIS is my dog"
print(a.upper()) #将所有字符串内所有的英文转变为大写

operation result:

 a.zfill The original string is right-aligned, and the front is filled with 0

a = "THIS is my dog"
print(a.zfill(20)) #从右侧填充0直到够20个字符

operation result

 a.strip removes the leftmost and rightmost spaces and extra characters in the string

a = "\t                 THIS is my dog        "
print(a.strip())  #去除多余空
print(a)    #未去除多余空  做个对比

operation result:

 a.lstrip removes extra spaces and tab keys on the left a.rstrip removes extra spaces and tab keys on the right

a = "\t                 THIS is my dog        "
print(a.lstrip()) #左边去掉多余的空格与tab键
print(a.rstrip()) #右边去多余的空格与tab键

 operation result:

 a.format references external variables

c = "my name is {name} i years old is {age}"
print(c.format(name="张三",age=22))
#在字符串内应用设定的变量值 但只有引用时才能有效果
c1 = "my name is {0} i years old is {1}"
print(c1.format("李四",22))
#这种写法类似于传参的方式 将format后面的括号内的内容从第0个开始传入到对应的地方

 operation result:

 Two, the string judgment method

overview

a.startswith #判断以什么开头   为真返回True  为假返回False
a.endswith  #判断以什么结尾
a.isalnum #是不是字母或者数字
a.isalpha #判断是不是字母  如果定义的值中有空格或者tab返回值也是False 
a.isdigit #判断是不是数字 只能判断整数
a.isascii #判断字符编码
a.isdecimal #如果字符串是否只包含十进制字符返回True,否则返回False。
a.isidentifier #是不是py语法关键词  是不是合法的可以做变量关键字
a.islower #判断是不是小写
a.isnumeric #是不是数字,跟isdigit有什么关系? 可以判断中文的数字 比如 二十五 三十五这样
a.isprintable #是否可以打印
a.isspace #是不是空格  
a.istitle # 是不是标题
a.isupper # 是不是全是大写

Detailed explanation

 a.startswith determines what starts with the string #If there are spaces at the beginning and end of the string, the result is False

a = "This is my cat"
print(a.startswith("Th"))  #字符串中有Th开头则 返回为True
print(a.startswith("TH"))  #定义的字符串中明显没有TH 所以返回的值肯定为 False
如果定义的字符串内开头有空格或者tab 可以添加参数strip

 operation result

a.endswith Determine what ends in the string #If there is a space at the beginning and end of the string, the judgment result is False

a = "   This is my cat  "
print(a.endswith("t")) #判断字符串中结尾的字符是否为“t”  #直接判断结果为False
print(a.strip().endswith("t")) #如果你定义的字符串结尾有空格可以这样做判断

operation result 

 

a.isalnum Determines whether a string is a number

a = "123com"
print(a.isalnum()) #如果字符中有数字与字母 结果为真
b = "123.456.qwe"
print(b.isalnum()) #如果有小数点或者特殊符号 结果为假

operation result:

a.isalpha Determines whether the string is a letter

b = "qwe"
print(b.isalpha()) #判断是否为字母

 operation result:

 a.isdigit judges whether the string is a number# can only judge integers

a = "123"
print(a.isdigit())  #整数为真
b = "123.456"
print(b.isdigit()) #非整数为假

operation result

a.isnumeric What is the difference between judging whether a string is a number and isdigit?

a = "十七"
print(a.isnumeric()) #判断中文数字的大写
b = "123.456"
print(b.isnumeric()) #如果不是整数 结果为假
c = "123"
print(c.isnumeric()) #判断是否为整数

operation result

a.isdecimal Returns True if the string contains only decimal characters, otherwise returns False.

a = "123"
print(a.isdecimal()) #如果只包含十进制数 结果为真
b = "123.456"
print(b.isdecimal()) #否则为假

 operation result

a.islower Determine whether the string is lowercase

a = " this is my cat "
print(a.islower()) #字符串为全小写 结果为真
b = "This is My cat" 
print(a.islower()) #如果字符串有大写 结果为假

 operation result

Is a.isupper all uppercase? Similar to the above

 a.isprintable Whether it can be printed

 a.isspace Is it a space 

a = " "
print(a.isspace()) #为空格 为真
b = " a b c "
print(b.isspace()) #否则为假
c = "\t"
print(c.isspace()) #\t 结果为真

operation result

a.istitle is title

a = "This is my cat"
print(a.istitle())

operation result 

 3. Check, modify, count, and replace strings

overview 

a.find # 查找字符串中的单个字符  是从左边开始找
a.rfind #  跟find一样 这个是从右开始找,并非左边
a.index ###比find好用  #  跟find功能是差不多,区别就是 find没有找到会返回-1的值 而index则会直接报错 
a.rindex # 与rfind是一样的,从右边开始找,跟index同理
a.count # 计数  可将字符串中某个字符出现的次数统计出来  ###可以选段操作
a.split #  默认按空格切割 将字符串内空格分开的所有字符分割为单个元素
a.rsplit # 与split同理 不过是从右边开始切割 同样可以使用maxsplit
a.splitlines # 切割元素
a.removeprefix #前缀某些字去掉
a.removesuffix #后缀某系子去掉 与removeprefix意思相同 区别就在于是从最后一个开始去除
a.replace #替换

Detailed explanation

a.find Find a single character in the string starting from the left

a = "This is my cat"
print(a.find("i")) #这里注意 只能从左边查到的第一个“i”的位置,第二个不会显示

print(a.find("is")) #也可以查找多个字符  返回的值为“i”所在的位置

print(a.find("i",3,10)) #截取的方式 意思是从第3个下标开始查找“i”的位置 直到第10个结束

print(a.find("d")) #如果查询没有则会返回-1的值 不会报错

operation result

 a.rfind is the same as find, the difference is that this is to find from the right

a = "This is my cat"
print(a.rfind("t")) #从最后一个字符开始找起 最后的结果是从第0个开始计数 直到找到“t”停

 operation result

a.index is similar to the search method of find, the difference is that if find cannot find a value, it will return -1, while index will directly report an error 

a = "This is my cat"

print(a.index("T")) #查找字符串中的“T”所在位置  
 
print(a.index("i",3,10)) #与find一致,可以截取位置查询

print(a.index("d")) #如果字符串中查到没有,则会报错

 operation result

 a.rindex # It is the same as rfind, start looking from the right, same as index 

a.count Counting can count the number of occurrences of a character in a string

a = "This is my cat"
print(a.count("i")) #查找字符串中i出现的次数 

print(a.count("i",3,10)) #从第三个位置开始(也就是s的位置) 查找“i”有几个,10可填可不填,不填默认到最后一个字符所在的位置结束

print(a.count("d")) #如果没有则会返回0的值

operation result

 a.split splits all characters separated by spaces in the string into a single element by default

a = "This is my cat"
print(a.split()) #以字符串中的空格作为切割,从而生成单个元素

print(a.split("i"))  #也可以已字母为切割点

print(a.split(maxsplit=1)) #可以设置切割次数 这里是从0开始算的

operation result

 a.rsplit is the same as split, but it starts cutting from the right and can also use maxsplit   

 a.splitlines cut with special symbols such as: \t \n .... etc.

a = "This\nis\nmy\ncat"

print(a) #直接打印会换行 作为一个对比

print(a.splitlines()) #以特殊字符切割

b = "This is my dog"

print(b.splitlines()) #如果字符串中没有特殊字符 那么只会有一个元素

 operation result

a.removeprefix Remove certain words from the prefix

a = "This is my dog"
print(a.removeprefix("This is")) #将字符串中“This is”去掉
print(a.removeprefix("Thisis")) #如果字符串中有空格你去掉的时候没有空格就会失效

operation result

a.removesuffix The removal of a certain suffix has the same meaning as removeprefix, the difference is that it is removed from the last one

a = "This is my dog"
print(a.removeprefix("dog")) #从前缀去除

print(a.removesuffix("dog")) #从后缀去除

operation result

 a.replace replace

a = "This is my dog dog"
print(a.replace("dog","cat"))#将dog 替换成cat 默认是全部替换

print(a.replace("dog","cat",1)) #这里可以只替换一个  是从左边开始的第一个

print(a.replace("d","#"))  #也可以换成特殊字符

operation result

4. Special usage of strings 

overview

a.encode #字符编码相关  
a.join #把列表转换成字符串 每个元素拼起来,按指定格式
a.maketrans # 生成密码本 
a.translate # 加密

Detailed explanation

a.join converts the list into a string and puts each element together in the specified format 

a = ["This","is","my","dog"]
print(" ".join(a)) #以空格连接列表中的字符串
print("_".join(a)) #也可以自定义连接符号

operation result:

a.maketrans # Generate codebook 

a.translate # encryption  

example:

import string  #导入工具
mi = string.printable[::-1] #将加密字符串倒转
mima = string.printable  #定一个正常的加密字符串
password = str.maketrans(mima,mi) #生成一个密码本
print(password)
a = "This is my dog"  #需要加密的内容
print(a.translate(password)) #查看加密后的样子
b = a.translate(password) #将加密的a字符串赋值给b
password1 = str.maketrans(mi,mima) #解密需要反着定义一个与刚才反着的密码本
print(b.translate(password1)) #将b的内容解密
#程序有个小bug 就说如果你直接将密文翻转加密的话,会出现同一个密码本能解开 而不需要从新翻转一下生成一个新的密码本 去解析 这样做是不安全的(正常的来说还需要通过些其他的加密算法去加密)  

operation result:

Guess you like

Origin blog.csdn.net/weixin_58279299/article/details/123383701