Python3 String
Strings are the most commonly used data type in Python. We can use quotation marks ( ' or " ) to create strings.
Creating a string is easy, just assign a value to the variable. E.g:
var1 = 'Hello World!' var2 = "TutorialFish"
Python Access Value in String
Python does not support single-character types, and single-characters are also used as a string in Python.
When Python accesses substrings, you can use square brackets [ ] to intercept strings. The syntax format of string interception is as follows:
variable [head index: tail index]
The index value starts with 0 , and -1 is the starting position from the end.
str = "TRY IT" str[0] = 'T' str[1] = 'R' str[2] = 'Y' str[3] = ' ' str[4] = 'I' str[5] = 'T' str[:] = 'TRY IT' str[0:] = 'TRY IT' str[:6] = 'TRY IT' str[:3] = 'TRY' str[0:2] = 'TR' str[1:4] = 'RY '
Examples are as follows:
Examples (Python 3+)
#!/usr/bin/python3 var1 = 'Hello World!' var2 = "Try It" print ("var1[0]: ", var1[0]) print ("var2[1:5]: ", var2[1:5])
Execution results of the above examples:
var1[0]: Hvar2[1:5]: ry I
Python String Update
You can intercept a part of the string and concatenate it with other fields, as in the following example:
Examples (Python 3+)
#!/usr/bin/python3 var1 = 'Hello World!' print("Updated string: ", var1[:6] + 'TutorialFish!')
Execution results of the above examples
Updated string: Hello TutorialFish!
Python Escape Characters
When you need to use special characters in characters, Python uses backslash \ to escape characters. The following table:
Escape Character | Description | Example |
---|---|---|
\ (At the end of the line) | Line continuation | >>> print("line1 \ ... line2 \ ... line3") line1 line2 line3 >>> |
\\ | Backslash | >>> print("\\")\ |
\' | apostrophe | >>> print('\'')' |
\" | Double quotes | >>> print("\"")" |
\a | Ring the bell | >>> print("\a")The computer makes a sound after execution. |
\b | Backspace | >>> print("Hello \b World!") Hello World! |
\000 | Empty | >>> print("\000") >>> |
\n | Wrap | >>> print("\n") >>> |
\v | Vertical tab | >>> print("Hello \v World!") Hello World! >>> |
\t | Horizontal tab | >>> print("Hello \t World!") Hello World!>>> |
\r | Press Enter. To move the content after \r to the beginning of the string, and replace the characters at the beginning one by one, until the content after \r is completely replaced. | >>> print("Hello\rWorld!") World! >>> print('google tutorialfish bing\r123456') 123456 tutorialfish bing |
\f | Page change | >>> print("Hello \f World!") Hello World! >>> |
\yyy | An octal number, y represents characters from 0 to 7, for example: \012 represents line feed. | >>> print("\110\145\154\154\157\40\127\157\162\154\144\41") Hello World! |
\xyy | Hexadecimal number, starting with \x, character represented by y, for example: \x0a represents newline | >>> print("\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21") Hello World! |
\other | Other characters are output in normal format |
Python String Operators
In the following table, the value of the variable a is the string "Hello", and the value of the b variable is "Python":
Operator | Description | Example |
---|---|---|
+ | String concatenation | a + b output: HelloPython |
* | Repeat output string | a*2 output result: HelloHello |
[] | Get the characters in the string by index | a[1] output result e |
[:] | To intercept a part of the string, follow the principle of left closed and right open , str[0:2] does not contain the third character. | a[1:4] output result ell |
in | Member operator - returns True if the string contains the given character | 'H' in a output is True |
not in | Member operator - returns True if the string does not contain the given character | 'M' not in a output result is True |
r/R | Original string-Original string: All strings are used directly literally, without escaping special or unprintable characters. The original string has almost the same syntax as the ordinary string except that the letter r is added before the first quotation mark of the string (upper and lower case). | print(r '\n') print(R '\n') |
% | Format string | Please see the next section. |
Examples (Python 3+)
#!/usr/bin/python3 a = "Hello" b = "Python" print("a + b output result:", a + b) print("a * 2 output result:", a * 2) print("a[1] output result:", a[1]) print("a[1:4] output result:", a[1:4]) if("H" in a): print("H in variable a") else: print("H is not in variable a") if("M" not in a): print("M is not in variable a") else: print("M is in variable a") print(r'\n') print(R'\n')
The output of the above example is:
a + b output result: HelloPythona * 2 output result: HelloHelloa[1] output result: ea[1:4] output result: ellH in variable aM is not in variable a\n\n
Python String Formatting
Python supports the output of formatted strings. Although very complex expressions may be used in this way, the most basic usage is to insert a value into a string with the string format character %s.
In Python, string formatting uses the same syntax as the sprintf function in C.
Example (Python 3+)
#!/usr/bin/python3 print("My name is %s %d years old this year!" % ('Jake', 10))
The output of the above example:
My name is Jake 10 years old this year!
Python string formatting symbols:
Symbol | Description |
---|---|
%c | Formatting characters and their ASCII codes |
%s | Format string |
%d | Format integer |
%u | Format unsigned integer |
%o | Format an unsigned octal number |
%x | Format an unsigned hexadecimal number |
%X | Format an unsigned hexadecimal number (uppercase) |
%f | Format floating point numbers, you can specify the precision after the decimal point |
%e | Format floating point numbers in scientific notation |
%E | Same function as %e, use scientific notation to format floating point numbers |
%g | Shorthand for %f and %e |
%G | Shorthand for %f and %E |
%p | Format the address of the variable with a hexadecimal number |
Format operator auxiliary instructions:
Symbol | Description |
---|---|
* | Define width or decimal point precision |
- | Used for left alignment |
+ | Show plus sign (+) in front of positive numbers |
<sp> | Display a space before positive numbers |
# | Display zero ('0') in front of the octal number, and display '0x' or '0X' in front of the hexadecimal number (depending on whether you are using 'x' or 'X') |
0 | The displayed number is filled with '0' instead of the default space |
% | '%%' outputs a single'%' |
(var) | Mapping variables (dictionary parameters) |
mn | m is the minimum total width of the display, n is the number of digits after the decimal point (if available) |
Starting from Python2.6, a new function str.format() has been added for formatting strings, which enhances the function of string formatting.
Python Triple Quotes
Python triple quotation marks allow a string to span multiple lines, and the string can contain line breaks, tabs, and other special characters. Examples are as follows:
Examples (Python 3+)
#!/usr/bin/python3 para_str = """This is an example of a multi-line string Multi-line strings can use tabs TAB (\t ). You can also use the newline character [\n ]. """ print (para_str)
The execution result of the above example is:
This is an example of a multi-line stringMulti-line strings can use tabsTAB ( ).You can also use the newline character [ ].
The triple quotation marks free programmers from the quotation marks and special strings, keeping a small piece of string format throughout the so-called WYSIWYG (What You See Is What You Get) format.
A typical use case is when you need a piece of HTML or SQL, then use string combination, special string escape will be very cumbersome.
errHTML = ''' <HTML><HEAD><TITLE> Friends CGI Demo</TITLE></HEAD> <BODY><H3>ERROR</H3> <B>%s</B><P> <FORM><INPUT TYPE=button VALUE=Back ONCLICK="window.history.back()"></FORM> </BODY></HTML> ''' cursor.execute(''' CREATE TABLE users ( login VARCHAR(8), uid INTEGER, prid INTEGER) ''')
f-string
f-string was added after python3.6. It is called a literal format string, which is the syntax of a new format string.
We used to use percent signs (%) before:
Example
>>> name = 'TutorialFish' >>> 'Hello %s' % name 'Hello TutorialFish'
The f-string format string starts with f and is followed by a string. The expression in the string is wrapped in curly braces {}, which will replace the calculated value of the variable or expression. Examples are as follows:
Example
>>> name ='TutorialFish' >>> f'Hello {name}' # Replace variable 'Hello TutorialFish' >>> f'{1+2}' # Use expression '3' >>> w = {'name': 'TutorialFish', 'url': 'www.tutorialfish.com'} >>> f'{w["name"]}: {w["url"]}' 'TutorialFish: www.tutorialfish.com'
In this way, it is obviously simpler, and there is no need to judge whether to use %s or %d.
In the Python 3.8 version, the = symbol can be used to concatenate operation expressions and results:
Example
>>> x = 1 >>> print(f'{x+1}') # Python 3.6 2 >>> x = 1 >>> print(f'{x+1=}') # Python 3.8 x+1=2
Unicode String
In Python2, ordinary strings are stored as 8-bit ASCII codes, while Unicode strings are stored as 16-bit unicode strings, which can represent more character sets. The syntax used is to prefix the string with u .
In Python3, all strings are Unicode strings.
Python String Built-In Functions
Python's string commonly used built-in functions are as follows:
Num | Method and Description |
---|---|
1 | capitalize() |
2 | center(width, fillchar) Returns a string with the specified width in the center, fillchar is the filled character, and the default is a space. |
3 | count(str, beg=0, end=len(string)) Returns the number of occurrences of str in string, if beg or end is specified, returns the number of occurrences of str in the specified range |
4 | bytes.decode(encoding="utf-8", errors="strict") There is no decode method in Python3, but we can use the decode() method of the bytes object to decode a given bytes object, which can be encoded and returned by str.encode(). |
5 | encode(encoding='UTF-8', errors='strict') Encode the string in the encoding format specified by encoding. If an error occurs, a ValueError will be reported by default, unless errors specify'ignore' or'replace' |
6 | endswith(suffix, beg=0, end=len(string)) |
7 | expandtabs(tabsize=8) Convert the tab symbol in the string to a space. The default number of spaces for the tab symbol is 8. |
8 | find(str, beg=0, end=len(string)) Check if str is contained in the string, if the range beg and end are specified, check if it is contained within the specified range, if it contains, return the starting index value, otherwise return -1 |
9 | index(str, beg=0, end=len(string)) Same as the find() method, except that an exception will be reported if str is not in the string. |
10 | isalnum() If the string has at least one character and all characters are letters or numbers, it returns True, otherwise it returns False |
11 | isalpha() If the string has at least one character and all characters are letters or international characters, it returns True, otherwise it returns False |
12 | isdigit() If the string contains only numbers, it returns True, otherwise it returns False. |
13 | islower() If the string contains at least one case-sensitive character, and all these (case-sensitive) characters are lowercase, return True, otherwise return False |
14 | isnumeric() If the string contains only numeric characters, it returns True, otherwise it returns False |
15 | isspace() If the string contains only blanks, it returns True, otherwise it returns False. |
16 | istitle() If the string is titled (see title()), it returns True, otherwise it returns False |
17 | isupper() If the string contains at least one case-sensitive character, and all these (case-sensitive) characters are uppercase, return True, otherwise return False |
18 | join(seq) Use the specified string as the delimiter to merge all the elements (the string representation) in seq into a new string |
19 | len(string) Returns the length of the string |
20 | ljust(width[, fillchar]) Return a new string with the original string left-aligned and filled to the length width with fillchar. The fillchar defaults to spaces. |
21 | lower() Convert all uppercase characters in the string to lowercase. |
22 | lstrip() Trim the spaces or specified characters on the left side of the string. |
23 | maketrans() Create a conversion table for character mapping. For the simplest call method that accepts two parameters, the first parameter is a string, which represents the character to be converted, and the second parameter is also a string that represents the target of the conversion. |
24 | max(str) Returns the largest letter in the string str. |
25 | min(str) Returns the smallest letter in the string str. |
26 | replace(old, new [, max]) Replace old in the string with new. If max is specified, the replacement will not exceed max times. |
27 | rfind(str, beg=0, end=len(string)) It is similar to the find() function, but searches from the right. |
28 | rindex( str, beg=0, end=len(string)) Similar to index(), but starts from the right. |
29 | rjust(width, [, fillchar]) Return a new string with the original string right-aligned and filled to the length width with fillchar (default empty space) |
30 | rstrip() Delete spaces or specified characters at the end of the string. |
31 | split(str="", num=string.count(str)) Use str as the separator to intercept the string, if num has a specified value, only num+1 substrings will be intercepted |
32 | splitlines([keepends]) Separate by lines ('\r','\r\n', \n'), and return a list containing each line as an element. If the parameter keepends is False, the newline character is not included, and if it is True, the newline character is retained. |
33 | startswith(substr, beg=0, end=len(string)) Check whether the string starts with the specified substring substr, if yes, return True, otherwise return False. If beg and end specify values, check within the specified range. |
34 | strip([chars]) Perform lstrip() and rstrip() on the string |
35 | swapcase() Convert uppercase to lowercase in a string, and lowercase to uppercase |
36 | title() Return a "titled" string, which means that all words start with uppercase and the rest of the letters are lowercase (see istitle()) |
37 | translate(table, deletechars="") Convert the characters of string according to the table given by str (containing 256 characters), and put the characters to be filtered out in the deletechars parameter |
38 | upper() Convert lowercase letters in a string to uppercase |
39 | zfill (width) Returns a string of length width, the original string is right-aligned, and the front is filled with 0 |
40 | isdecimal() Check whether the string contains only decimal characters, if it is true, otherwise it returns false. |