Python-based string manipulation

Source: Internet
Author: User

The following display code is implemented in Ipython3

s='i like Python'  

#首字母大写

Capitalize ()
1 in [3]: s='i like Python'23 in [4]: s.capitalize () 4 ' I like Python '

#全部转换大写, convert all lowercase

Upper () lower ()

In [6]: S.upper () out['I like PYTHON'

1 in [7]: S.lower ()2'i like Python'

#大写转换, convert the original uppercase to lowercase, lowercase to uppercase

In [8]: s.swapcase () out['I like PYTHON'

#单词首字母大写

In [9]: S.title () out['I like Python'

#居中

In [All]: S.center (#默认是空格Out) ['   i like    python'
in [+]: S.center ('~') #制定字符串Out ['~~~i like python~~~~ '

s = ' I like Pyhon '
#删除前后空格 (the default is to delete spaces, you can specify the front and back strings to delete)

Strip
in [+]: S.strip () out['i like  pyhon'

#删除后边空格

Rstrip
in [+]: S.rstrip () out['  i like  pyhon'

#删除左边空格

Lstrip
in [+]: S.lstrip () out['i like  pyhon  '

#公共方法

#获取字符串元素长度

Len
in [+]: Len (s) out[19]: 17

#查看元素是否以某字符串开头 (return to True,false),

StartsWith
EndsWith
In []: S.startswith (") out[]: True

In []: S.endswith (") out[]: True

#根据元素找索引号 (Returns-1 if not found)

Find

In [23°c]: S.find ('i') out[23]: 2

Index (Error if not found)

in [+]: S.index ('i') out[26]: 2

#统计 (returns 0 if the element is not counted)

Count

In []: S.count ('i') out[29]: 2

#替换

in [+]: s.replace ('like','love') out['    I love  pyhon  '

Python-based 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.