"We learn python together"-string manipulation

Source: Internet
Author: User

Here is a list of some of the operation of the string, convenient for later use when the query.

1.capitalize
Print ("My name is Galen". Capitalize ()) #首字母大写My name is Galen

2.center

Print ("My name is Galen". Center (30, "-")) #字符串居中显示30个字节, the other sections are filled with "-" lines ("My name is Galen The string is left with 30 bytes, and the rest is filled with a "-" line to print ("My name is Galen"). Rjust ((, "-")) #字符串居右显示30个字节, the other parts are filled with "-" lines-------My name is Galen-------My name is Galen----------------------------my name is Galen

3.endswith, StartsWith

Print ("My name is Galen". EndsWith ("my")) #判断字符串是否一my结尾, if true, is not returned Falseprint ("My name is Galen". StartsWith ("my")) #判断字符串是否一my开始, if True is returned, the falsefalsetrue is not returned

4.find RFind

Print ("My name is Galen". Find ("NA")) #如果子字符在母字符串中有多个, the index of the first character of the rightmost substring print ("My name is Gnalen". RFind ("NA")) # If the substring has more than one character in the parent string, the index of the first character of the rightmost substring, 312#, uses find and slice mates to remove some characters from the string str1 = "My name is Galen" find = "Me" if find in str1:n = Str1.find (find) L = Len (str1) str1 = Str1[:n]+str1[n+len (find):] Print (str1) My na is Galen

5.count

Print ("My name is Galen". Count ("M")) #计算出字符在该字符串中出现的次数1

6.encode,decode

To_byte = "My name is Galen, I love the Motherland". Encode (encoding= "GBK") #将字符串从编码成二进制形式, encoding specifies the encoded form of the original string print (to_byte) to_str = to_ Byte.decode (encoding= "GBK") #将二进制解编码成字符串, here the encoding is converted into binary Yes code, otherwise it will error print (TO_STR) b ' My name is galen\xa3\xac\xce\ Xd2\xb0\xae\xd7\xe6\xb9\xfa ' My name is Galen, I love the motherland

7.format,format_map

str2 = "My name is {name},i am {age} years old" print (str2) print (Str2.format (name= "Galen", age= ")") #字符串格式化print ( Str2.format_map ({"Name": "Galen", "Age": "$"})) #字符串格式化, the element in the dictionary can be introduced to the My name is {name},i am {age} years oldmy name is Galen, I am years oldmy name is galen,i am years

8.index

Print ("My name is Galen". Index ("NA") #返回字符串 the first character of the "Na" in the string, and if there are more than one, returns the first character at the index of the number one on the left, print ("My name is Galena". Rindex ( "NA")) #返回字符串 the index value of the first character of "Na" in the string, and if there are more than one, the first character is returned at the index 315 of the number one on the right.

9.isalnum

Print ("12as". Isalnum ()) #如果该字符串中只有阿拉伯数字和字符则返回True, returns Falseprint ("[Email protected]" If it contains special characters. Isalnum ()) # Returns true if only Arabic numerals and characters are in the string, or False if a special character is included

10.isalpha

Print ("ASFCD". Isalpha ()) returns True #如果字符串中只含有英文字母 (including uppercase), otherwise returns Falseprint ("Asfd1". Isalpha ())

11.isdigit

Print ("123". IsDigit ()) #如果该字符串是否整数则返回True, otherwise return falseprint ("123a". IsDigit ()) #print ("123.1". IsDigit ()) #

12.isidentifier

Print ("_123a". Isidentifier ()) #判断是不是一个合法的标识符 (variable name), returns True if it can be the variable name, otherwise return falseprint ("123.1". Isidentifier ()) # The judgment is not a valid identifier (variable name)

13.islower

Print ("str1". Islower ()) #如果字符串都是小写则返回True, and the inverse returns Falseprint ("Str1". Islower ()) #

14.isnumeric

Print ("12.12". IsNumeric ()) #判断字符串中是否只有数字 if only the number returns true, otherwise Falseprint ("1212") is returned. IsNumeric ())

15.isspace

Print ("Isspace ()") #判断该字符是否是个空格, yes returns true, not return Falseprint ("". Isspace ()) #判断该字符是否是个空格, yes returns true, not return false

16.title

Print ("My name is Galen". Istitle ()) #判断该字符串是否是标题 (that is, the first character of each word is capitalized), and if it is true, returns Falseprint ("My name S Galen". Istitle ()) Print ("My name is Galen". Title ()) #将该字符串转换成标题, converts the first letter of each word to uppercase

17.isprintable

Print ("MY NAME". isprintable ()) #判断该字符串是否能打印, can print return true, cannot print return false, device files in Linux cannot be printed and return false

18.isupper

Print ("My Name". Isupper ()) #判断该字符串是否全是大写, full capitalization returns True, otherwise Falseprint ("My Name") is returned. Isupper ())

19.join

Print ("_". Join ("123")) #将前一个字符串插入后面的字符串中. Print ("_". Join (["Galen", "Jack", "Mark"]) #将前一个字符串插入后面的字符串中.

20.lower,upper

Print ("Galen". Lower ()) #将字符串中的大写变成小写print ("Galen". Upper ()) #将字符串中的小写变成大写

21.strip

Print ("Galen". Lstrip ()) #去掉字符左端的换行和回车print ("\ngalen". Lstrip ()) #去掉字符左端的换行和回车print ("Galen". Strip ()) # Remove line breaks at both ends of the character and enter print ("Galen \ n". Rstrip ()) #去掉字符右端的换行和回车print ("----")

22.translate,maketrans

Str_mak = Str.maketrans ("abcdefghijkg", "[email protected]") #前面的字符串和后面的字符串一一对应print ("Galen". Translate (Str_mak)) # Replace the string with the corresponding relationship between the two strings in Str_mak

23.replace

Print ("My name is Galen". Replace ("n", "n")) #将一个字符串换层另外一个字符串, by default, replaces all old strings with the new string print ("My name is Galen". Replace (" n "," n ", 1)) #可以限制替换的个数, in order to replace print (" My name is Galen ") from front to back. Replace (" Na "," NA ")

24.split,splitlines

Print ("My name is Galen". Split ()) #将字符串分割 and saved in a list, by default, by dividing print with spaces ("My_name_is_galen"). Split ("_")) #这次以下划线进行分割print ("My_name_is_galen". Split ("_", 1)) #这次以下划线进行分割, but only 1 times, from the left to split the print ("My_\nname_is\n_galen". Splitlines ()) # Split by line break

25.swapcase

Print ("My Name is Galen". Swapcase ()) #将大写字母换成小写, lowercase letters in uppercase

26.zfill

Print ("My Name is Galen". Zfill) #在原字符串的左边用 0 padding until the entire character length is 50 characters


This article is from the "Pure mash Eight cup vsit" blog, please be sure to keep this source http://chunlaobazhong.blog.51cto.com/11499723/1956611

"We learn python together"-string manipulation

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: info-contact@alibabacloud.com 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.