Python string-related method rollup __python

Source: Internet
Author: User
Tags base64 lowercase

Python Capitalize () methodDescribe
Python capitalize () capitalizes the first letter of the string, and the other letters become lowercase. For 8-bit byte encoding needs to be based on the local environment.
Grammar
Capitalize () method syntax:
Str.capitalize ()
Parameters
No.
return value
This method returns a string with the first letter capitalized.


Python Center () methodDescribe
Python Center () returns a new string that is centered with the original string and padded with spaces to length width. The default padding character is a space.
Grammar
Center () method Syntax:
Str.center (width[, Fillchar])
Parameters
Width-The total width of the string.
Fillchar--padding character.
return value
The method returns a middle of the original string and fills the new string with a space to length width.




Python cmp () functionDescribe
The CMP (x,y) function is used to compare 2 objects if x < y returns 1 if x = = y returns 0 if x > y returns 1.
Grammar
The following is the syntax for the CMP () method:
CMP (x, y)
Parameters
X--Numeric expression.
Y--numeric expression.
return value
If x < y returns-1, if x = = y returns 0, if x > y returns 1.


Python count () methodDescribe
The Python count () method is used to count the number of occurrences of a character in a string. The optional parameter is the start and end position of the string search.
Grammar
Count () method syntax:
Str.count (Sub, start= 0,end=len (String))
Parameters
Sub--substring of search
Start-the position at which the string begins the search. The default is the first character, and the first character index value is 0.
End--the position in the string that ends the search. The index of the first character in the character is 0. The default is the last position of the string.
return value
This method returns the number of times the substring appears in the string.


Python Decode () methodDescribe
The Python decode () method decodes the string in a encoding-specified encoding format. The default encoding is string encoding.
Grammar
Decode () method syntax:
Str.decode (encoding= ' UTF-8 ', errors= ' strict ')
Parameters
Encoding-the encoding to use, such as "UTF-8".
Errors--Sets the processing scheme for different errors. Default is ' strict ', meaning coding error causes a unicodeerror. Other possible values are ' ignore ', ' replace ', ' xmlcharrefreplace ', ' backslashreplace ' and any value registered through Codecs.register_error ().
return value
The method returns the decoded string.
Instance
The following example shows an instance of the Decode () method:
#!/usr/bin/python
str = "This is string Example....wow!!!";
str = str.encode (' base64 ', ' strict ');
Print "encoded String:" + str;
Print "decoded String:" + str.decode (' base64 ', ' strict ')
The results of the above example output are as follows:
Encoded string:dghpcybpcybzdhjpbmcgzxhhbxbszs4uli53b3chise=
Decoded string:this is String example....wow!!!




Python Encode () methodDescribe
The Python Encode () method encodes strings in the encoded format specified by encoding. The errors parameter can specify different error handling scenarios.
Grammar
Encode () method syntax:
Str.encode (encoding= ' UTF-8 ', errors= ' strict ')
Parameters
Encoding-the encoding to use, such as "UTF-8".
Errors--Sets the processing scheme for different errors. Default is ' strict ', meaning coding error causes a unicodeerror. Other possible values are ' ignore ', ' replace ', ' xmlcharrefreplace ', ' backslashreplace ' and any value registered through Codecs.register_error ().
return value
The method returns the encoded string.
Instance
The following example shows an instance of the Encode () method:
#!/usr/bin/python
str = "This is string Example....wow!!!";
Print "encoded String:" + str.encode (' base64 ', ' strict ')
The results of the above example output are as follows:
Encoded string:dghpcybpcybzdhjpbmcgzxhhbxbszs4uli53b3chise=




Python EndsWith () methodDescribe
The Python EndsWith () method is used to determine whether a string ends with a specified suffix, and false if the end of the specified suffix returns true. The optional arguments "start" and "end" are the starting and ending positions of the retrieved string.
Grammar
EndsWith () method syntax:
Str.endswith (suffix[, start[, end])
Parameters
Suffix--this argument can be a string or an element.
Start-The starting position in the string.
End--the position at which the character ends.
return value
Returns False if the string contains the specified suffix and returns true.
Instance
The following example shows an instance of the EndsWith () method:
#!/usr/bin/python
str = "This is string Example....wow!!!";
suffix = "wow!!!";
Print str.endswith (suffix);
Print Str.endswith (suffix,20);


suffix = "is";
Print str.endswith (suffix, 2, 4);
Print str.endswith (suffix, 2, 6);
The results of the above example output are as follows:
True
True
True
False


Python Expandtabs () methodDescribe
The Python expandtabs () method turns the tab symbol (' \ t ') in the string to a space, and the default number of spaces for the tab sign (' \ t ') is 8.
Grammar
Expandtabs () method syntax:
Str.expandtabs (tabsize=8)
Parameters
Tabsize--Specifies the number of characters in the conversion string that the tab symbol (' \ t ') converts to a space.
return value
This method returns a new string that is generated when the tab symbol (' \ t ') in the string is converted to a space.
Instance
The following example shows an instance of the Expandtabs () method:
#!/usr/bin/python
str = "This is\tstring example....wow!!!";


Print "Original string:" + str;
Print "Defualt exapanded tab:" + str.expandtabs ();
Print "Double exapanded tab:" + str.expandtabs (16);
The results of the above example output are as follows:
Original string:this is string EXAMPLE....WOW!!!
Defualt exapanded tab:this is string EXAMPLE....WOW!!!
Double exapanded tab:this is string EXAMPLE....WOW!!!



Python Find () methodDescribe
The Python find () method detects if the string contains substring str, if you specify a beg (start) and end (ending) range, check if it is contained within the specified range, or return 1 if the containing substring returns the index value of the start.

Grammar
Find () method syntax:
Str.find (str, beg=0, End=len (String))
Parameters
STR--Specifies the retrieved string
Beg--Start index, defaults to 0.
End--the ending index, which defaults to the length of the string.
return value
Returns 1 if the containing substring returns the index value of the start.

Instance
The following example shows an instance of the Find () method:


Instance (Python 2.0+)
#!/usr/bin/python
STR1 = "This is string Example....wow!!!";
STR2 = "Exam";

Print Str1.find (STR2);
Print Str1.find (str2, 10);
Print Str1.find (str2, 40);
The results of the above example output are as follows:
15
15
-1
Instance (Python 2.0+)
>>>info = ' ABCA '
>>> Print Info.find (' a ') # starts with subscript 0, looks for the first substring that appears in the string, and returns the result: 0
0
>>> Print Info.find (' A ', 1) # starts with subscript 1, looking for the first substring to appear in the string: return result 3
3
>>> Print Info.find (' 3 ') # Cannot find return-1
-1
>>>


Python Index () methodDescribe
The Python Index () method detects whether the string contains substring str, and if the beg (start) and end (ending) range is specified, the check is included in the specified range, as is the case with the Python find () method, except that if STR is not in string, it will report a An exception.


Grammar
Index () method syntax:
Str.index (str, beg=0, End=len (String))
Parameters
STR--Specifies the retrieved string
Beg--Start index, defaults to 0.
End--the ending index, which defaults to the length of the string.
return value
Throws an exception if the containing substring returns the index value of the start.


Python Isdecimal () methodDescribe
The Python Isdecimal () method checks whether the string contains only decimal characters. This method exists only in Unicode objects.
Note: Define a decimal string, just add a ' u ' prefix before the string.
Grammar
Isdecimal () method syntax:
Str.isdecimal ()
Parameters
No
return value
Returns False if the string contains only decimal characters and returns true.


Python len () methodDescribe
The Python len () method returns the length of an object (character, list, tuple, and so on) or the number of items.
Grammar
Len () method syntax:
Len (s)
Parameters
S--object.
return value
Returns the length of the object.


Python Ljust () methodDescribe
The Python ljust () method returns the left alignment of the original string and fills the new string with a space to the specified length. Returns the original string if the specified length is less than the length of the original string.
Grammar
Ljust () method syntax:
Str.ljust (width[, Fillchar])
Parameters
Width--Specifies the length of the string.
Fillchar--padding characters, default to spaces.
return value
Returns a left-aligned string of original strings and fills the new string with a space to the specified length. Returns the original string if the specified length is less than the length of the original string.


Python Lower () methodDescribe
The Python lower () method converts all uppercase characters in a string to lowercase.
Grammar
Lower () method syntax:
Str.lower ()
Parameters
No.
return value
Returns a string that is generated when all uppercase characters in a string are converted to lowercase.


Python Lstrip () methodDescribe
The Python Lstrip () method is used to truncate a space or a specified character to the left of the string.
Grammar
Lstrip () method syntax:
Str.lstrip ([chars])
Parameters
Chars--Specifies the intercepted character.
return value
Returns a new string that is generated after a space is truncated to the left of the string or a specified character.


Python Rstrip () methodDescribe
Python Rstrip () deletes the specified character at the end of the string string (the default is a space).
Grammar
Rstrip () method syntax:
Str.rstrip ([chars])
Parameters
Chars--Specifies the deleted character (default is a space)
return value
Returns a new string that is generated after the specified character at the end of a string string is deleted.


Python Splitlines () methodDescribe
The Python splitlines () is delimited by line (' \ r ', ' \ r \ n '), returns a list that contains each row as an element, or if the argument keepends is False, does not contain a newline character, and if true, preserves the line break.
Grammar
Splitlines () method syntax:
Str.splitlines ([keepends])
Parameters
Keepends--whether to remove newline characters (' \ r ', ' \ r \ n ', \ n ') in the output result, default to False, no line break, and, if true, leave line breaks.
return value
Returns a list that contains each row as an element.


Python startswith () methodDescribe
The Python StartsWith () method checks whether the string starts with the specified substring, returns True if it is, or returns FALSE. If the parameter beg and end specify a value, it is checked within the specified range.
Grammar
StartsWith () method syntax:
Str.startswith (str, Beg=0,end=len (string));
Parameters
STR--the detected string.
Strbeg-An optional parameter is used to set the starting position of string detection.
Strend-An optional parameter is used to set the end position of the string detection.
return value
Returns True if a string is detected, otherwise it returns false.


Python Strip () methodDescribe
The Python Strip () method removes the character specified by the string (the default is a space).
Grammar
Strip () method syntax:
Str.strip ([chars]);
Parameters
Chars--Removes the character specified by the string and its tail.
return value
Returns a new string that removes the character generation specified by the string.


Python swapcase () methodDescribe
The Python swapcase () method converts the uppercase and lowercase letters of a string.
Grammar
Swapcase () method syntax:
Str.swapcase ();
Parameters
NA.
return value
Returns a new string generated after uppercase and lowercase letters have been converted.




Python Title () methodDescribe
The Python title () method returns a "caption" string, which means that all words start with uppercase and the remaining letters are lowercase (see istitle ()).
Grammar
Title () method Syntax:
Str.title ();
Parameters
NA.
return value
Returns a "heading" string, which means that all words start with uppercase.


Python Translate () methodDescribe
The Python translate () method converts characters of a string according to the table given by the parameter table (256 characters), and the characters to be filtered are placed in the Del parameter.

Grammar
Translate () method syntax:
Str.translate (table[, Deletechars]);
Parameters
Table-translation tables, translation forms are converted by Maketrans method.
Deletechars--the list of characters to filter in the string.
return value
Returns the translated string.
Instance
The following examples show how the translate () function is used:


#!/usr/bin/python
From string import Maketrans # refers to the Maketrans function.
Intab = "Aeiou"
Outtab = "12345"
Trantab = Maketrans (Intab, Outtab)

str = "This is string Example....wow!!!";

Print str.translate (trantab);


The results of the above example output are as follows:

TH3S 3s str3ng 2x1mpl2....w4w!!!


The following instance removes the ' x ' and ' m ' characters from the string:
#!/usr/bin/python
From string import Maketrans # Required to call Maketrans function.

Intab = "Aeiou"
Outtab = "12345"
Trantab = Maketrans (Intab, Outtab)

str = "This is string Example....wow!!!";

Print str.translate (Trantab, ' XM '); above instance output result:

TH3S 3s str3ng 21pl2....w4w!!!



Python Upper () method Description
The Python Upper () method converts lowercase letters in a string to uppercase letters.
Grammar
Upper () method syntax:
Str.upper ()
Parameters
NA.
return value
Returns a string of lowercase letters converted to uppercase letters.


Python Zfill () method Description
The Python Zfill () method returns a string of the specified length, the original string is right-aligned, and the front padding is 0.
Grammar
Zfill () method syntax:
Str.zfill (width)
Parameters
Width--Specifies the length of the string. The original string is right-aligned, and the front padding is 0.
return value

Returns a string of the specified length.



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.