Python: string

Source: Internet
Author: User
Tags ming

string: Enclose the single quotation mark (') or double quotation mark (") and use a backslash (\) to escape the special Character.

Creation and assignment of strings:

' HelloWorld '  = str(= str ([1,2,3,4,5])  # converts the list to a string str1 = str ((1,2,3,4,5))  #  convert tuples to Strings
string Access:
str ='Runoob'Print(str)#Output StringPrint(str[0:-1])#outputs all characters from the first to the penultimatePrint(str[0])#the first character of the output stringPrint(str[2:5])#outputs the characters from the third start to the fifthPrint(str[2:])#output all characters from the beginning of the thirdPrint(STR * 2)#output String two timesPrint(str +"TEST")#Connection String

Output result:

Runoobrunoornoonoobrunoobrunoobrunoobtest
Escape Character:
\ (at End of line): continuation character \ \: backslash \ ' : Single quotation mark \b: backspace \ n: line break \ R: Enter \f: page change \v: portrait tab \ t: Transverse tab

For example:

>>>Print('let\ ' s go!')#escaped with escape character "\"Let's go!or:>>>Print("let ' s go!") example: Output C:\now path>>> str ='C:\\now'       #escape with escape character>>>Print(str) C:\now#If there are many such \ times, for example: C:\Program files\intel\wifi\help
#原始字符的使用非常简单, just precede the string with an English letter R>>> str ='C:\now'>>>Print(str) C:ow>>> str = r'C:\now' #after adding R>>>Print(str) C:\now
String formatting:
String formatting:% (%s: formatted string%d: formatted integer,%c: formatted character and its ASCII code,%f: formatting floating point

Cases:

Print (" my name is%s this year is%d! ") " % (' Xiao Ming ', 10))

Output result:

My name is Xiao Ming this year 10 years old!
String built-in Functions:
Capitalize ():#converts the first character of a string to uppercaseCenter (width, fillchar):#returns a string that specifies the width of the center, Fillchar to a filled character, and a space by defaultCount (str, beg= 0,end=len (string)):#returns the number of occurrences of STR in a string, if beg or end specifies that the number of STR occurrences in the specified range is returnedDecode (encoding='UTF-8', errors='Strict'):#decodes a string by using the specified encoding. The default encoding is string encodingEndsWith (suffix, beg=0, End=len (string)):#checks whether the string ends with obj, or returns false if beg or end specifies that the specified range is terminated with obj, if it is trueExpandtabs (tabsize=8):#turns the tab in string into a space, the default number of spaces in the tab sign is 8Find (str, beg=0 End=len (string)):#detects if STR is contained in a string, and if beg and end specify a range, the check is contained within the specified range, and returns 1 if the index value of the start is returned .Index (str, beg=0, End=len (string)):#just like the Find () method, only if STR does not report an exception in the stringIsalnum ():#returns true if the string has at least one character and all characters are letters or numbers, otherwise falseIsalpha ():#returns true if the string has at least one character and all characters are letters, otherwise falseIsDigit ():#returns True if the string contains only numbers otherwise FalseIslower ():#returns true if the string contains at least one case-sensitive character, and all of these (case-sensitive) characters are lowercase, otherwise falseIsNumeric ():#returns True if the string contains only numeric characters, otherwise falseIsspace ():#returns True if the string contains only spaces, otherwise falseIstitle ():#returns True if the string is a caption, otherwise falseSupper ():#returns True if the string contains at least one case-sensitive character, and all of these (case-sensitive) characters are uppercase, otherwise falseJoin (seq):#merges all elements of the SEQ (the string Representation) into a new string with the specified string as a delimiter>>> temp ='I love You'>>> Temp.join ('*!')'*i Love you!'>>> Temp.join ('**!')'*i Love you*i love you!'>>> Temp.join ('***!')'*i love you*i love you*i love you!'Len (string):#return string lengthLower ():#uppercase letters in converted strings are lowercaseUpper ():#Lowercase letters in a converted string are capitalizedLjust (width[,fillchar]):#returns the left alignment of an original string and fills the new string with the width of length using fillchar, Fillchar The default is a spaceRjust (width,[,fillchar]):#returns the right alignment of an original string and fills a new string of length width with Fillchar (default space)Lstrip ():#remove spaces to the left of a stringRstrip ():#remove spaces at the end of a string stringMax (str):#returns the largest letter of the string strMin (str):#returns the smallest letter in the string strReplace (old,new[,max]):#Replace the str1 in the string with str2, if Max is specified, the replacement does not exceed Max times>>> temp ='I love You'>>> Temp.replace (' love',' love')'I love You'rfind (str, Beg=0,end=len (string)):#similar to the find () function, but looking from the rightSplit (str="", Num=string.count (str)):#num=string.count (str)) intercepts a string with the Str delimiter, and only the NUM substring if NUM has a specified value>>> str1 ='HelloWorld'>>> Str1.split ('o')['Hell','W','Rld']splitlines (num=string.count ('\ n')):#Returns a list that contains rows as elements, separated by rows, if NUM specifies that only num rows are slicedStartsWith (str, Beg=0,end=len (string)):#checks whether the string starts with obj, returns true, or False. If beg and end specify a value, the check is within the specified rangeStrip ([chars]):#remove spaces around a string>>> str1 ='Hellowor LD'>>>str1'Hellowor LD'>>>str1.strip () swapcase ():#convert uppercase in a string to lowercase, lowercase to uppercaseTranslate (table, deletechars=""):#converts a string character to a table (containing 256 characters) given by str, and the character to be filtered out into the Deletechars parameterZfill (width):#returns a string of length width, the original string right-aligned, front padding 0Isdecimal ():#checks whether the string contains only decimal characters and returns False if True
String

Python: string

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.