Summary of string built-in methods in Python

Source: Internet
Author: User

#!/usr/local/bin/python3#-*-coding:utf-8-*-name= "My wife is Mahongyan" #----------capitalized----------#print ( Name.capitalize ()) #----------character Statistics----------#print (Name.count (' m ')) #----------character beautiful print----------#print ( Name.center ((), '-') #一共打印50个字符, if not enough, use '-' on either side of the string----------to determine what the string ends with----------#print (Name.endswith (' Yan ')) # Determines whether the string ends with ' Yan ', if it is returned true#----------tab conversion----------#name1 = "My \twife is Mahongyan" #print (Name1.expandtabs (                            tabsize=30)) #将字符串中的tab the key to the specified number of spaces #----------Remove the Subscript----------' Print (Name.find (' wife ')) #取字符串 ' wife ' the first character in the string subscript #注: 1. Starting from 0, a space bar is counted as a # 2. If the string contains two ' wife ', it returns the subscript value in the first ' wife ' print (name[ Name.find (' wife '): 7]) #切片操作, start slicing from the subscript where ' W ' is, until ' e ', then remove ' wife ' ' #----------formatted----------' name2= ' my wife is {name} And she is {age} years old "Print (Name2.format (name= ' Mahongyan ', age=26)) print (Name2.format_map ({' name ': ' Mahongyan ', ' Age ': ') #参数为字典格式 ' #----------determine if the string is a pure Arabic numeral + character----------#print (Name.isalnum ()) #注: Is ' pure ' Arab numberWords and characters, if there are spaces or other special characters in the string return false#----------determine if the string is a plain English character----------#print (Name.isalpha ()) #如果字符串中含有数字或其他非英文字符, Returns false#----------determines whether the string is a decimal number----------"name3=" 0100 "Print (Name3.isdecimal ()) #注: If the string is a binary number, it also returns true ' # '----- -----Determine if the string is a valid identifier (variable name)----------"name4=" _1x "Print (Name4.isidentifier ())" #---------- Determines whether a string is an integer type----------"name5=" "Print (Name5.isdigit ())" #----------determines whether the string is lowercase----------#print ( Name.islower ()) #字符串中可以含有数字或特殊字符, that is, only determine if the letter contained in the string is lowercase #----------determine if the string is uppercase----------#print (Name.isupper ()) # The string can contain numbers or special characters, that is, only if the letters contained in the string are uppercase #----------determine if the string is a ' pure ' number----------#print (Name.isnumeric ()) #有小数点也不行, with IsDigit No big difference #----------determine if a string is a space----------' name6= ' Print (Name6.isspace ()) #一个到n个空格都返回True ' #---------- Determines whether the string is in the title format----------' name7= ' My Wife is Mahongyan ' Print (Name7.istitle ()) #即每个单词的首字母均为大写 ' #---------- Converts a string to a header format----------#print (Name.title ()) #----------Join usage----------#print (' * '. Join ([' A ', ' B ', ' C ')) #尚未理解 #----- -----The end of a string to fill the print----------#print (Name.ljust (50, "-")) #使输出的字符串长度为50, if the string length is not enough, the trailing '-' #----------string header fills the print----------#print (Name.rjust (), "-") #使输出的字符串长度为50, If the string length is not sufficient, the first padding '-' #----------converts the letters in the string to uppercase/lowercase----------' Print (Name.upper ()) #只转换所有的字母print (Name.lower ()) "#- ---------Remove the space on the left or right side of the string or enter----------"Print (" \nabc ". Lstrip ()) #去除左侧print (" ABC \ n ". Rstrip ()) #去除右侧print (" \NABC \ n " . Strip ()) #去除两侧 ' #----------Encrypt 1----------' P=str.maketrans ("ABCDEFG", "1234567") #将 "ABCDEFG" corresponds to "[email  protected]#$%^& "Print (" bad ". Translate (p)) #将密码规则p pass in the Translate function and convert" bad "to the encrypted string ' #---------- Encrypt 2----------#print (Name.translate (Str.maketrans ("ABCDEFG", "1234567")) #效果和1一样 #----------Replace----------' Print ( Name.replace (' A ', ' a ')) #将字符串中的所有的字符 ' A ' replaced by ' a ' Print (Name.replace (' A ', ' a ', 2)) #将字符串中的前两个 ' a ' replaced with ' a '---------- Search----------#print (name.rfind (' y ')) #从左往右遍历字符串并返回字符 ' Y ' the largest subscript in the string #----------conversion list----------' Print (Name.split () #将字符串按照 a space and place it in a list print (' 1+2+3+4 '). Split (' + ')) #将字符串按照字符 ' Y ' split and put in a list in turn, but the character ' Y ' disappears print ("1\n2\n3\n4 ". Splitlines ()) #将字符串按照换行符分割, and put in a list" #----------to find----------"Print (Name.startswith (' My)) # Look in the string for the existence of the string ' my ' if there is a return Trueprint (Name.startswith (' my ', 0,4)) #在字符串下标为0 to find out if there is a string ' my ', if there is a return true ' #---------- The letter case Conversion----------#print (Name.swapcase ()) #将字符串中的大写字母转换成小写, lowercase letters are converted to uppercase #----------16 binary----------#print (name.zfill

  

Summary of string built-in methods in Python

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.