python-Common String Operations

Source: Internet
Author: User

name = ' Shanbaoliang.exe '

print (Name.capitalize ()) #将字符串首字母大写

print (, '-') #把字符串居中 and fills the number with a specific character

Print (Name.count (' Liang ')) #统计字符出现次数

Print (name.endswith (' exe ')) #判断字符串是否以XX结尾, returns TRUE or false

Print (Name.startswith (' Shan ')) #判断字符串是否以XX开头, returns TRUE or false

print (Name.upper ()) #大写所有字符
print (Name.lower ()) #小写所有字符


Print (Name.find (' K ')) #找到返回第一次出现的下标, could not find return-1
#print (Name.index (' K ')) #找到返回第一次出现的下标, error not found, not practical

print (Name.isdigit ()) #判断是否为纯数字, returns TRUE or false

print (Name.isidentifier ()) #判断是否是合法的变量名, returns TRUE or FALSE, not practical

print (Name.isspace ()) #判断是否全为空格, all returns True

aa= ' abc123 '
bb= ' abc123 single '

print (Aa.isalpha ()) #是否只有英文字母
print (Bb.isalnum ()) #是否只包含数字, English letter, Kanji, return true or False

cc= ' abcbbccaabc '
Dd=cc.strip (") #去掉字符串两边的东西, do not write by default remove spaces and line breaks
#print (DD)

#print (Cc.lstrip ()) #去掉左边字符
#print (Cc.rstrip ()) #去掉右边字符

Print (Cc.replace (' abc ', ' Leungo ', 1)) #字符串替换, replace ABC with Leungo, number means replace several times, do not write full replacement

ee= ' 5 '
Print (Ee.zfill (2)) #在前边补0, can be defined to complement a few 0

names= ""
" "
Import String
print (string.ascii_letters) #所有大小写字母
print (string.ascii_lowercase) #所有小写字母
print (string.ascii_uppercase) #所有大写字母
print (string.digits) #所有数字
print (string.punctuation) #所有特殊字符

#ctrl + Left view function source code


The string is converted into a list:

Import String #导入string函数
Print (String.digits.split ())
Nums=list (string.digits) #nums为字符串
Method 1:
For S in Nums:
Num.append (s)
Method 1:
Nums = List (string.digits)
Method 3:
Print (Names.split ()) #1, split string 2, turn the string into a list, by default separated by spaces and line breaks

names= ' Shanbl,wangdz,zhouyf,zhangz,niuhy '
Print (Names.split (', ')) #不传 ', ' by default, splits only one element;

The list is converted into a string:
list1=[' SHANBL ', ' wangdz ', ' zhouyf ', ' zhangz ', ' Niuhy ']
Print ('/'. Join (List1)) #把list变成字符串, and concatenate with a character, return a string, Ps:join is a string method, note the usage

python-Common String Operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.