Python Development Basics-String Type Tutorial (String method)-1

Source: Internet
Author: User

s = ' Hello world! '

Print (S.capitalize ()) #第一个字母大写, remaining lowercase
# Output: Hello world!
Print (S.swapcase ()) #大写变小写, lowercase to uppercase
#输出: HELLO world!
Print (S.casefold ()) #全变小写
#输出: Hello world!
Print (S.center, '-') #S字符字符串在总50宽度的居中位置, with "-" padding on both sides
#输出:-------------------Hello World!-------------------
Print (S.count (' l ')) #统计字符串中有多少个 ' l '
#输出: 3
Print (S.count (' l ', 0,5)) #统计字符串中从第一个字母到第5个字母, how many ' l '
#输出: 2
Print (S.endswith (' d ')) #判断字符串是否已 ' d ' end
#输出: FALSE
Print (S.expandtabs ()) #定义TAB键字符的宽度
Print (S.find (' O ')) #查找一个字符或字符串的位置 (index), not found, returned-1
#输出:, 4
Print (S.find (' dfdf '))
#输出:-1
S3 = ' My name si {0},i am {1} years old '
Print (S3.format (' Zhang San ', +)) #格式化输出
#输出: My name si Zhang San,i am years old
S3 = ' My name si {name},i am {age} years old ' #格式化支持定义占位符
Print (S3.format (name= ' Zhangsan ', age=23)) #给占位符赋值

Python Development Basics-String Type Tutorial (String method)-1

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.