One, handling strings
Special symbols
▎ How do I use special symbols within a string?
>>> text1= ' His cat's name is Tom. ' File "<stdin>", line 1 text1= ' s cat's name is Tom. ' ^syntaxerror:invalid syntax>>> text1= ' His cat\ ' s name was Tom. ' # # "\" escape character >>> text2= "His cat's name is Tom." # #可以使用双引号开始和结束 >>> print (R ' s Cat\ 's name is Tom. ') # #原始字符串His cat\ ' s name is Tom.>>> Text1 "his cat's name is Tom." >>> Text2 "His cat's name is Tom."
▎ Escape Character:
\ ' Single quotation mark
\ "Double quotation marks
\ t tab
\ n line break
\ \ Inverted Slash
>>> print ("\this Cat's name is \ntom.") His cat ' s name istom.
Sanchong quotation marks
The starting and ending of a multiline string is 3 single or 3 double quotation marks. All quotation marks, tabs, or line breaks between "triple quotes" are considered part of the string. The code block indentation rules for Python do not apply to multi-line strings.
Multi-line strings are also often used as multiline annotations.
>>> print ("' Hello,... Can you find my world .... via Tom. Hello,can you find my World.via Tom.
string slices
Like a list of strings, use subscripts and slices.
The string slice does not modify the original string.
>>> text1= ' His cat\ 's name is Tom. ' >>> Text1[:10] "his cat's" >>> text2= "his cat's name is Tom." >>> Text2[:10] "his cat S"
In and not in
Tests whether the first string (exact match) is in the second string.
>>> text1= ' His cat\ 's name is Tom. ' >>> "Cat" in Text1true>>> "Tom" not in Text1true
Second, the String method
Uppercase and lowercase
The upper () and lower () string methods return a new string in which all the letters of the original string are converted accordingly to uppercase or lowercase. The non-alphabetic characters of the string remain unchanged.
>>> text1= ' His cat\ 's name is Tom. ' >>> Text1.upper () "His CAT's NAME is TOM." >>> Text1.lower () "His cat's name is Tom." >>> Text1 # #这些方法没有改变字符串本身, instead returns a new string. "His cat's name is Tom."
The Isupper () and Islower () methods are used to determine whether all letters in a string are uppercase or lowercase.
>>> ' Hello '. Islower () false>>> ' Hello '. Isupper () false>>> ' Hello '. Upper (). Isupper () True >>> ' Hello '. Lower (). Islower () true>>> ' Hello123 '. Lower () islower () true>>> ' Hello123 '. Upper (). Isupper () True
▎ Other common methods of judging strings:
Isalpha () returns True if the string contains only letters and is not empty;
Isalnum () returns True if the string contains only letters and numbers, and is not empty;
Isdecimal () returns True if the string contains only numbers and is not empty;
Isspace () returns True if the string contains only spaces, tabs, and line breaks, and is not empty;
Istitle () returns True if the string contains only first-letter uppercase words followed by lowercase letters;
Start and end
The StartsWith () and EndsWith () methods return True if the string they are calling starts or ends with the string passed in by the method. Otherwise, the method returns false.
>>> text1= ' His cat\ 's name is Tom. ' >>> Text1.startswith (' his cat ') true>>> Text1.startswith (' his cat ') false>>> Text1.endswith (' Om. ') True>>> text1.endswith ('. ') True
Insert and split
The join () method is called on a string, and the argument is a string list that returns a string. The returned string is concatenated by each string in the passed-in list.
A string that invokes the join () method, which is inserted into the middle of each string in the list parameter.
The join () method is called for a string and passes in a list value.
>>> ' # '. Join ([' Tom ', ' Jack ', ' Mike ', ' Alice ']) ' tom#jack#mike#alice ' >>> ' \ n '. Join ([' Tom ', ' Jack ', ' Mike ', ' Alice ']) ' Tom\njack\nmike\nalice '
The split () method returns a list of strings for a string call.
The default is split by a variety of whitespace characters, such as spaces, tabs, or line breaks.
A common split () usage is to split multi-line strings according to line breaks.
>>> text ' Tom\njack\nmike\nalice ' >>> text.split () [' Tom ', ' Jack ', ' Mike ', ' Alice ']>>> Text.split (' i ') [' tom\njack\nm ', ' ke\nal ', ' CE ']
Align the books
The Rjust () and Ljust () string methods return the populated version of the string that invokes them, aligning the text by inserting an empty glyd.
The first parameter of these two methods is an integer length that is used to align the string. The Second optional parameter is to specify a fill character instead of a space character.
The center () string method is similar to Rjust () and Ljust (), but it makes the text centered.
Use these three methods to ensure that strings are neatly aligned, even if you don't know how many characters are there.
>>> ' Hello '. Rjust ' hello ' >>> ' hello '. Ljust ' hello ' >>> ' hello '. Center ( '-'-------Hello--------' >>> ' Hello '. Rjust (ljust) ' Hello ' >>> ' hello ' rjust (10). Ljust ('-') ' Hello----------'
Remove white space characters
The Strip () string method returns a new string that has no white space characters at the beginning or end of it.
The Lstrip () and Rstrip () methods will delete the left or right white space characters accordingly.
There is an optional parameter that specifies which characters on both sides should be deleted.
>>> text= ' Hello World ' >>> text.strip () "Hello World" >>> text.lstrip () ' Hello World ' >& Gt;> Text.rstrip () ' Hello World ' >>> text.strip (' Held ') ' O Wor '
Copy paste String
The Pyperclip module has the copy () and paste () functions, which can send text to or receive text from the computer's clipboard.
The ▎pyperclip module is not in Python and needs to be installed.
[[email protected] src]# wget https://bootstrap.pypa.io/get-pip.py ...] 2017-07-15 03:11:16 (24.4 kb/s) - saved "get-pip.py" [1595408/1595408]) [[ email protected] src]# python get-pip.py ... Successfully installed pip-9.0.1 setuptools-36.2.0 wheel-0.29.0[[email protected] src]# pip -Vpip 9.0.1 from /usr/lib/python2.7/site-packages (python 2.7) [[email protected] src]# pip install pyperclipcollecting pyperclip Downloading pyperclip-1.5.27.zipBuilding wheels for collected packages: pyperclip running setup.py bdist_wheel for pyperclip ... done stored in directory: /root/.cache/pip/wheels/0b/fe/d7/ 1ab1ec7a91dc707d04b872214f6ce617f1b04a027de12fd4fesuccessfully built pyperclipinstalling Collected packages: pyperclipsuccessfully installed pyperclip-1.5.27
>>> Import pyperclip>>> pyperclip.copy (' Hello World ') >>> pyperclip.paste () ' Hello World '
This article is from the "garbled Age" blog, please be sure to keep this source http://juispan.blog.51cto.com/943137/1947923
[Python 3 Series] string manipulation