Python Basics (str Class)

Source: Internet
Author: User

Second, string (str Class)

Tip: All of the following methods are methods in the class, the first argument is self, and unity is not written.


The methods that are included are:

1, capitalize () #首字母变大写

>>> name= ' Hello World '

>>> name.capitalize ()

' Hello World '


2, center (width, fillchar=none)#内容居中, Width: total length; Fillchar: padding content in white space, default None

>>> name= ' Hello World '

>>> Name.center (20, ' # ')

' # # # #hello world##### '


3, COUNT (X,start=none, End=none)#子序列x的个数 parameters can be connected to start and end positions

>>> name= ' ABCDD '

>>> name.count (' d ', 0,9)

2

>>> name.count (' d ', 0, 3)

0

>>> name.count (' d ', 0,4)

1


4. Find (X,start=none, end=none)#查找子序列x的位置, returns 1 if not found

>>> name= ' ABCD '

>>> name.find (' a ')

0

>>> name.find (' d ')

3

>>> name.find (' e ')

-1


5, index (X,start=none, end=none)#查找子序列x的位置, if not found the exception

>>> Name.index (' a ')

0

>>> Name.index (' d ')

3

>>> name.index (' e ')

Traceback (most recent):

File "<stdin>", line 1, in <module>

Valueerror:substring not found


6, EndsWith (x)#是否以x结束

>>> name= ' ABCD '

>>> name.endswith (' a ')

False

>>> name.endswith (' d ')

True


7, StartsWith (x)#是否以x开始

>>> name= ' ABCD '

>>> name.startswith (' a ')

True

>>> name.startswith (' d ')

False


8, Expandtabs (tabsize=none)#将tab转换成空格, the default tab is converted to 8 spaces

>>> name= ' AB CD '

>>> Name.expandtabs (4)

' AB CD '


9. Format ()#字符串格式化, 4 ways

9-1:

Str1= ' I am {0}, age {1} '

Str1.format (' Alex ', 24)

9-2:

Str2= ' I am {SS}, age {DD} '

Str2.format (ss= ' Alex ', dd=24)#类似于函数的形参

9-3:

li=[' Alex ', 24]

Str3= ' I am {0}, age {1} '

Str3.format (*li)#列表的元素个数必须和需要的参数相等

9-4:

dic={' SS ': Alex, ' DD ': 24}

Str4= ' I am {SS}, age {DD} '

Str4.format (**dic)#字典的key必须和形参的名称一致


10, Isalnum ()#是否是字母和数字

>>> name= ' ABCD '

>>> Name.isalnum ()

True


11, Isalpha ()#是否是字母

>>> name= ' ABCD '

>>> Name.isalpha ()

True


12, IsDigit ()#是否是数字

>>> name= ' ABCD '

>>> Name.isdigit ()

False


13, Islower ()#是否小写 Lower () is to change uppercase in a string to lowercase

>>> name= ' ABCD '

>>> Name.islower ()

True

>>> name= ' ABCD '

>>> Name.islower ()

False


14, Isupper ()#是否是大写 Upper () is to capitalize the lowercase in the string

>>> name= ' ABCD '

>>> Name.isupper ()

False

>>> name= ' ABCD '

>>> Name.isupper ()

True


15, Istitle ()#是否是标题 title () is to set the string as the title, that is, the first letter of each word capitalized

>>> name= ' Hello World '

>>> Name.istitle ()

False


16, Isspace ()#是否是空格

>>> name= '

>>> Name.isspace ()

True


17. Join ()#连接 Returns a string, in contrast to split

>>> name= ' ABCD '

>>> ' _ '. Join (name)

' A_b_c_d '

>>> name=[' A ', ' B ', ' C ', ' d ']

>>> ' _ '. Join (name)

' A_b_c_d '


18, Split (Sep,maxsplit=none)#分割, maxsplit up a few times, the default is full split, the return value is a list

>>> name= ' a,b,c,d '

>>> name.split (', ')#全分割

[' A ', ' B ', ' C ', ' d ']

>>> name.split (', ', 2)#只分割前两个字符串

[' A ', ' B ', ' c,d ']


19, Ljust (width, fillchar=none)#内容左对齐, width is wide, the right padding the specified character, fillchar default is empty rjust () is right-aligned, syntax format

>>> name= ' ABCD '

>>> Name.ljust (20, ' # ')

' abcd################


20, Lstrip ()#移除左侧空白 rstrip () Remove the right blank strip () remove both sides of the blank

>>> name= ' ABCD '

>>> Name.lstrip ()

' ABCD '

>>> Name.rstrip ()

' ABCD '

>>> Name.strip ()

' ABCD '


21. Partition (Sep)#以sep为分割点 Division, before, in, and after three parts

>>> name= ' abc '

>>> name.partition (' B ')

(' A ', ' B ', ' C ')


22, replace (old,new,count=none)#替换, the Count parameter is the specified number of replacements, the default is all replace

>>> name= ' ABCD '

>>> name.replace (' A ', ' e ')

' EBCD '

>>> name= ' 1122334444 '

>>> name.replace (' 4 ', ' 5 ', 2)#替换2个4

' 1122335544 '

>>> name.replace (' 4 ', ' 5 ', 3)#替换3个4

' 1122335554


23, Swapcase ()#大写变小写, lowercase to uppercase

>>> name= ' ABCD '

>>> Name.swapcase ()

' AbCD '


24, translate (table, Deletechars=none)#字符转换, you need to make a corresponding table table,deletechars default is empty, if a character is specified, the specified character will be deleted

Import string

Intab = "Aeiou"

Outtab = "12345"

Trantab = String.maketrans (Intab, Outtab)

STR1 = "This is string EXAMPLE....WOW!!!"

Print str.translate (Trantab, ' XM ')#str1中的含有转换表中的字符都会被转换, the X and M characters will be deleted.

Output result: th3s 3s str3ng 21pl2....w4w!!!


Next article

Lists (List Class)

This article from "A rookie on the Sky" blog, please be sure to keep this source http://rmeos.blog.51cto.com/761575/1710205

Python Basics (str Class)

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.