Python string manipulation

Source: Internet
Author: User

Tag: Action end string Operation Pytho Strip intercept Mat string nbsp

1. Two types of string formatting

String%s number%d

Example Name= ' I am%s '% ' FKK '

Print (name)

' I am FKK '

>>> name= ' I am%s,age%d '% (' daoxin ', +) >>> name ' I am daoxin,age 13 '

The second type:

Index starting from 0, value Index name= ' I am {0},age{1} '

>>> Name.format (' Daoin ', 20)

' I am daoin,age20 '

Intercept string

#左边包含, the right side does not contain

Name= ' Daoxin '

#截取第一个字符串
Print (Name[0])
#从第一个字符开始, intercept two
Print (Name[0:2])
#从第几个字符开始, to the end of the string
Print (name[1:])
#从最后一个开始, intercept the last character
Print (Name[-1]) ==print (len (name)-1)
# #从最后一个开始, intercept all the previous
Print (Name[:-1])
#字符串长度
Print (len (name))

Remove the whitespace method:

Name= ' Daoxin '
#去掉两边空格
Print (Name.strip ())
#去掉左边空格
Print (Name.lstrip ())
#去掉右边空格
Print (Name.rstrip ())

Python string manipulation

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.