Python: String Common functions

Source: Internet
Author: User

name = "My name \tis {name} and {age} years old"
Print (Name.capitalize ()) #第一个字母大写
Print (Name.title ()) #字符串中每个单词的首字母均大写
Print (Name.count ("a") #) # Number of arguments passed in the string
Print (Name.center, "-") #字符串长度为50, not enough to fill '-'
Print (Name.ljust ("*")) #字符串长度为50, not enough to the right to fill '-'
Print (Name.rjust ("*")) #字符串长度为50, not enough left to fill '-'
Print (Name.endswith ("ex")) #检查字符串是否以传入参数为结尾
Print (Name.startswith ("my")) #检查字符串是否以传入参数为开头
Print (Name.expandtabs (tabsize=30)) #更改tab所占空格
Print (Name.find ("name")) #返回传入的参数在该字符串中的索引值
Print (Name.rfind ("a")) #找到最右边值为a的下标并返回
Print (Name.format (name = "Alex", age = All)) #字符串格式化
Print ({"Name": "Alex", "Age": Name.format_map}) #字符串格式化, passed in as dictionary
Print ("Ab23". Isalnum ()) #检查字符串是否在所有英文字母及阿拉伯数字中
Print ("AbA". Isalpha ()) #检查字符串是否为纯英文字符
Print ("0". IsDigit ()) #判断是否为非负整数, similar to the IsNumeric () function
Print ("A1". Isidentifier ()) #判断是否为合法的标识符
Print ("". Isspace ()) #判断是否为空格
Print ("LoA". Islower ()) #是否小写
Print (Name.lower ()) #字母变为小写
Print ("ABC". Isupper ()) #是否大写
Print (Name.upper ()) #字母变为大写
Print ("I am L". Istitle ()) #字符串中每个单词首字母是否大写
Print ("AB". Isprintable ()) #是否可打印 (TTY files and drive files are not printable)
Print ("+". Join (["1", "3", "3"]) #最好用于字符串分割列表 (the element in the list must be a string)
Print ("\nab c \ n". Strip ()) #去除两边的空格或回车
Print ("\nab c \ n". Lstrip ()) #去除最左边的空格或回车
Print ("\nab c \ n". Rstrip ()) #去除最右边的空格或回车

p = Str.maketrans ("ABCDEFG", "1234567") #将第一个参数的值替换为第二个参数的值, with translate
Print ("Alex". Translate (p))

Print ("Alex Li". replace ("L", "a", 1)) #将字符串中值为第一个参数的替换为第二个参数, value 1 means replace one, do not write replace all
Print ("1+2+3+4". Split ("+")) #以传入的参数为值分割该字符串, returns a list if no parameter is separated by a space by default
Print ("Ajkdj\ndjfkdjf". Splitlines ()) #相当于split ("\ n"), which is a newline. Note: Some operating systems have line-break characters \ r \ n
Print ("Lex Li". swapcase ()) #大写变小写, lowercase to uppercase

Python: String Common functions

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.