2016.07.09-10 String method

Source: Internet
Author: User

String

Unicode sequence
The string is immutable

Definition of string: Supports subscript operation, supports slicing operation, supports unpacking, packet operation .
s = ' magedu '

Method of String:

join:
Join (...)
S.join (iterable), str

uses S to stitch up each character element of an iterative object (only the character elements), returns a string, and S is a connector.
>>> lst = [' I ', ' love ', ' You ']
>>> ". Join (LST) #使用空格拼接字符串
' I love you '
>>> ', '. Join (LST) #使用 ', ' stitching string
' i,love,you '
>>>


Span style= "color: #ff0000;" >split:
Split (...) method of Builtins.str instance
S.split (Sep=none, maxsplit=-1), List of strings
left-to-right, delimited by delimiters, Sep is a delimiter, default is ' ', the separator can be a single character or a string, but not a regular expression, you can use the specified delimiter, the string is delimited, the second argument is delimited, 1 (the default) is delimited by all.

>>> s
' My name is ZJ '
>>> s.split ()
[' My ', ' name ', ' is ', ' ZJ ']
>>> S.split (", 1)
[' My ', ' name is ZJ ']
>>> s.split (" ', 2)
[' My ', ' name ', ' is ZJ ']
>>> s.split (',-1)
[' My ', ' name ', ' is ', ' ZJ ']

>>> f = open ('/etc/passwd ', ' R ')
>>> a = F.readlines ()
>>> for I in a:
... UserName, *_, bash = i.split (': ')
.. print (Username,bash)
...
Root/bin/bash

Bin/sbin/nologin

Daemon/sbin/nologin

Adm/sbin/nologin

Lp/sbin/nologin

rsplit:
Rsplit (...) method of Builtins.str instance
S.rsplit (sep= None, Maxsplit=-1), List of strings

is similar to the split method, but rsplit from right to left, separated by delimiters, Sep as a delimiter, default to ", you can use the specified delimiter to separate the string, The second parameter is the number of separators, 1 (the default) is delimited for all.

>>> s
' My name is ZJ '
>>> s.rsplit ()
[' My ', ' name ', ' is ', ' ZJ ']
>>> s.rs
S.rsplit (S.rstrip (
>>> s.rsplit (', 1)
[' My name is ', ' ZJ ']
>>> s.rsplit (' ', 2)
[' m Y name ', ' is ', ' ZJ ']
>>> s.rsplit (",-1)
[' My ', ' name ', ' is ', ' ZJ ']
>>> *_, name = S.rsplit (' ', 1)
>>> name
' ZJ '
>>>

Splitlines:
Splitlines (...)
S.splitlines ([keepends]), List of strings
Separated by rows, returns a list of string elements, keepends defaults to False, does not preserve newline characters, and true indicates that line breaks are preserved
>>> test = "My name is ZJ
... I Love Python
... I Want coding my Program
... ‘‘‘
>>> Test
' My name is Zj\ni love Python\ni want coding my program\n '
>>> Test.splitlines ()
[' My name is ZJ ', ' I love Python ', ' I Want coding my program ']
>>> Test.splitlines (True)
[' My name is zj\n ', ' I love python\n ', ' I want coding my program\n ']
>>>


Partition
Partition (...)
S.partition (Sep) (head, Sep, tail)
Separates the string, Sep is a delimiter, and returns a tuple of three elements.
>>> line = ' url:http://www.baidu.com '
>>> line.partition (': ')
(' url ', ': ', ' http://www.baidu.com ')


Rpartition:
Rpartition (...)
S.rpartition (Sep) (head, Sep, tail)
Separates strings, separates from right to left, Sep is a delimiter, and returns a tuple of three elements.
>>> line.rpartition (': ')
(' Url:http ', ': ', '//www.baidu.com ')
>>>


String modification (Natural language modification):
Capitalize:
Capitalize (...)
S.capitalize () str
Handle the beginning of a string with the first letter capitalized
>>> s.capitalize ()
' My name is ZJ '
>>>

Title
Title (...) method of Builtins.str instance
S.title () str
Capitalize the first letter of each word of the string.
>>> S.title ()
' My Name is Zj '
>>>


Lower
Lower (...) method of Builtins.str instance
S.lower () str
Turns all uppercase letters in a string into lowercase.
>>> S.lower ()
' My name is ZJ '
>>>


Upper
Upper (...) method of Builtins.str instance
S.upper () str
capitalizes all lowercase letters of a string.
>>> S.upper ()
' MY NAME is ZJ '
>>>


Swapcase:
Swapcase (...) method of Builtins.str instance
S.swapcase () str
The lowercase letters in a string are capitalized, and uppercase letters become lowercase.
' My name is ZJ '
>>> S1 = S.title ()
>>> S1
' My Name is Zj '
>>> S1.swapcase ()
' MY NAME is ZJ '

Center
Center (...) method of Builtins.str instance
S.center (width[, Fillchar]) str
Center The string output, width, specify the string width, fillchar as the padding, default to spaces, optional parameters.
>>> S.center (50)
' My name is ZJ '
>>> S.center (50, ' # ')
' ################# #my name is zj################### '
>>>

Ljust:
Ljust (...) method of Builtins.str instance
S.ljust (width[, Fillchar]) str
The string is right-padded, L is left, and valid characters are located on the leftmost side of the entire string.
>>> S.ljust (30)
' My name is ZJ '
>>> S.ljust (30, ' # ')
' My name is zj################# '
>>>


Rjust:
Rjust (...) method of Builtins.str instance
S.rjust (width[, Fillchar]) str
The string is left-padded, R is right, and the valid characters are located on the entire string.
>>> S.rjust (30)
' My name is ZJ '
>>> S.rjust (30, ' # ')
' ################ #my name is ZJ '
>>>


Zfill:
Zfill (...) method of Builtins.str instance
S.zfill (width), str
Fills the left side of the string with 0, with the width specified as the length of the character.
>>> S.zfill (30)
' 00000000000000000my name is ZJ '
>>>

Strip:
Strip (...) method of Builtins.str instance
S.strip ([chars]), str
The default is to delete whitespace (', \ n,-t) on either side of the string, specifying the characters specified on both sides of the parameter chars, and not specifying chars as the default delete white space.
>>> A
' \ n Zhang San li si \ t \ n '
>>> A.strip ()
' Zhang San li si '
>>>
>>> a.strip ('! ')
' This is!!! A!!! Test
>>>

Rstrip:
Rstrip (...)
S.rstrip ([chars]), str
The default is to delete the white space (', \ n,-T) to the right of the string, specifying the character specified on the right of the chars when the parameter is removed, and not specifying chars as the default delete white space.
>>> a = ' \ n Zhang San li si \ t \ n '
>>> A.rstrip ()
' \ n Zhang San li si '
>>> a = '!!!!! This is!!! A!!! Test!!!!!!! '
>>> a.rstrip ('! ')
‘!!!!! This is!!! A!!! Test

lstrip:
Lstrip (...)
S.lstrip ([chars]), str
defaults to removing whitespace (", \ n,-T) to the left of the string, specifying that the character specified on the left side of the parameter chars be deleted, and not specifying chars as the default delete white space.
>>> a = ' \ n Zhang San li si \ t \ n '
>>> a.lstrip ()
' Zhang San li si \ t \ n '
>>> A = '!!!!! This is!!! A!!! Test!!!!!!! '
>>> a.lstrip ('! ') The
' This is!!! A!!! Test!!!!!!! '


startswith:
StartsWith (...)
S.startswith (prefix[, start[, end]), BOOL
Determines whether the header is a specified character, prefix is the specified first character, the default is the first match from the string, [, start[, end]] is an optional parameter, The value is the index subscript, which indicates the specified matching region and returns a boo type.
>>> a = ' # # #test!!!! '
>>> a.startswith (' # ')
True
>>> a.startswith (' t ', 3, 5)
True
>>> A.startswith (' # ', 3, 5)
False
>>> a.startswith (' # ', -4,-1)
False
>>> a.startswith ('! ') ,-4)
True
>>>


Endstwith:
EndsWith (...)
S.endswith (suffix[, start[, end]), BOOL
Determines whether the end is a specified character, suffix is the specified trailing character, the default from the string last match, [, start[, end]] is an optional parameter, the value is index subscript, indicating the specified matching area, return a boo type.
>>> A
' # # #test!!!! '
>>> a.endswith ('! ')
True
>>> a.endswith (' t ', 4, 6)
False
>>> A[4:6]
' Es '
>>> A[4:7]
' EST '
>>> a.endswith (' t ', 4, 7)
True
>>> a.endswith ('! ', 4)
True
>>>


String Lookup substitution:
Count
Count (...)
S.count (sub[, start[, end]), int
As with the count usage of the list, the number of characters specified in the statistics string, returns an integer type, the sub is the specified character, [, start[, end]] is an optional parameter, the value is index subscript, specifies the area of the statistic.
>>> a = ' aaabbbaaabbbcccaaa '
>>> a.count (' a ')
9
>>> a.count (' A ', 4)
6
>>> a.count (' A ', 4, 10)
3
>>>


Find
Find (...)
S.find (sub[, start[, end]), int
Finds the position of the first occurrence of the specified character in the string (from left to right), returns an integer (index subscript), sub is the specified character, [, Start[, is an optional parameter, the value is the index subscript, specifies the lookup area, or 1 if there is no specified character in the string.

RFind:
RFind (...)
S.rfind (sub[, start[, end]), int
Finds the position of the first occurrence of the specified character in the string (from right to left), returns an integer (index subscript), sub is the specified character, [, Start[, is an optional parameter, the value is the index subscript, specifies the lookup area, or 1 if there is no specified character in the string.
>>> A
' Aaabbbaaabbbcccaaa '
>>> a.rfind (' B ')
11
>>> a.rfind (' BBB ')
9
>>> a.rfind (' BBB ', 0, 10)
3
>>> a.rfind (' BBB ', 0, 20)
9
>>> a.rfind (' BBB ', 0, 1)
-1
>>>


index:
Index (...)
S.index (sub[, start[, end]), int
usage As with find, the only difference to find is that the Find method returns a-1 when the specified character is not found. The index throws a ValueError exception.
>>> a
' aaabbbaaabbbcccaaa '
>>> a.index (' B ')
3
>>> a.index (' BBB ')
3
>>> a.index (' BBB ', 4, Ten)
Traceback (most recent call last):
File "<stdin>", line 1, in <modu Le>
valueerror:substring not found
>>> a.index (' BBB ', 4,)
9


rindex:
Rindex (...)
S.rindex (sub[, start[, end]), int
usage as with RFind, the only difference from RFind is that when the specified character is not found, the RFind method returns a-1, The rindex throws a ValueError exception.
>>> a
' aaabbbaaabbbcccaaa '
>>> a.rindex (' B ')
One
>>> a.rindex (' BBB ')
9
>>> a.rindex (' BBB ', 4, Ten)
Traceback (most recent call last):
File "<stdin>", line 1, in &L T;module>
valueerror:substring not found
>>> a.rindex (' BBB ', 4,)
9
>>> A.rindex ( ' B ', 4,

>>>


Replace
Replace (...)
S.replace (old, new[, Count]), str
Replaces the specified character in the string, returns the new string after the replacement is complete, does not modify the original string, old is the specified replacement character, new is the replacement character, Count is an optional parameter, the default full string match substitution, and specifies the number of times that count is replaced from left to right.
>>> A
' Aaabbbaaabbbcccaaa '
>>> a.replace (' aaa ', ' 123 ')
' 123bbb123bbbccc123 '
>>> a.replace (' aaa ', ' 123 ', 1)
' 123bbbaaabbbcccaaa '
>>> a.replace (' aaa ', ' 123 ', 2)
' 123bbb123bbbcccaaa '
>>>

2016.07.09-10 String method

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.