Python 3 strings
Python 3 Strings
Strings are the most commonly used data type in Python. We can use quotation marks (‘ or “) to create strings.
Creating a string is simple: just assign a value to a variable. For example:
var1 = 'Hello World!'
var2 = "Geekdoc"
Accessing String Values in Python
Python does not support single-character types; single characters are also treated as strings in Python.
To access substrings in Python, you can use square brackets to truncate a string, as shown in the following example:
Example (Python) 3.0+)
#!/usr/bin/python3
var1 = 'Hello World!'
var2 = "Geekdoc"
print ("var1[0]: ", var1[0])
print ("var2[1:5]: ", var2[1:5])
The above example execution results:
var1[0]: H
var2[1:5]: eekd
Python string update
You can intercept part of the string and concatenate it with other fields, as shown in the following example:
Example (Python 3.0+)
Python escape characters
When special characters are needed within a string, Python uses the backslash () escape character. The following table shows the escape character:
Escape character | Description |
---|---|
(at the end of a line) | Line continuation character |
Backslash symbol | |
’ | Single quotation mark |
” | Double quotation mark |
a | Bell |
b | Backspace |
00 | Space |
n | Line Feed |
v | Vertical Tab |
t | Horizontal Tab |
r | Carriage Return |
f | Form Feed |
oyy | Octal number, yy represents the character, for example: o12 represents Line Feed, where o is a letter, not a number 0. |
xyy | Hexadecimal number, yy represents a character, for example: x0a represents a line break. |
other | Other characters are output in normal format. |
Python String Operators
In the following example, variable a holds the string “Hello” and variable b holds “Python”:
Operator | Description | Example |
---|---|---|
+ | String concatenation | a + b Output: HelloPython |
* | Repeatedly outputs a string | a*2 Output: HelloHello |
[] | Get characters in a string by index | a[1] Output: e |
[ : ] | Intercepts a portion of a string. Following the left-closed, right-opened principle, str[0:2] does not contain the third character. | a[1:4] Output: ell |
in | Membership operator – Returns True if the string contains the given character | ‘H’ in a Outputs True |
not in | Membership Operator – Returns True if the string does not contain the given character. | ‘M’ not in a Outputs True |
r/R | Raw Strings – Raw strings are strings that are interpreted literally, without escaping special or unprintable characters.
Raw strings have almost identical syntax to regular strings, except that the letter r (uppercase or lowercase) is placed before the first quote mark. |
print(r'n') print(R'n') ) |
% | Format string | See the next section. |
Example (Python 3.0+)
#!/usr/bin/python3
a = "Hello"
b = "Python"
print("a + b output:", a + b)
print("a * 2 output:", a * 2)
print("a[1] output:", a[1])
print("a[1:4] output:", a[1:4])
if( "H" in a) :
print("H is 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 above example outputs:
a + b output: HelloPython
a * 2 output: HelloHello
a[1] output: e
a[1:4] output: ell
H is in variable a
M is not in variable a
n
n
Python string formatting
Python supports formatted string output. Although this may require very complex expressions, 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 C. The syntax is the same as the sprintf function in Python.
Example (Python 3.0+)
#!/usr/bin/python3
print ("My name is %s and I'm %d years old this year!" % ('Xiao Ming', 10))
The above example outputs:
My name is Xiao Ming and I'm 10 years old this year!
Python string formatting symbols:
Symbol | Description | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
%c |
%s |
Formatting string |
%d |
Formatting integer |
%u |
| %o |
Formatting an unsigned octal number | %x |
Formatting an unsigned hexadecimal number | %X
| Formats an unsigned hexadecimal number (uppercase) | %f
| Formats a floating-point number, optionally specifying the number of decimal places |
%e
| %E |
Same as %e, formats a floating point number using scientific notation. |
%g |
Shorthand for %f and %e |
%G
| Shorthand for %f and %E | %p
| Formats the address of a variable using hexadecimal numbers | |
Formatting operator auxiliary instructions:
Symbol | Function |
---|---|
* | Defines width or decimal point precision |
– | Used for left alignment |
+ | Display a plus sign (+) before positive numbers |
Display a space before positive numbers | |
# | Display a leading zero (‘0’) for octal numbers, or ‘0x’ or ‘0X’ (depending on whether ‘x’ or ‘X’ is used) for hexadecimal numbers |
0 | Displays leading zeros instead of spaces |
% | ‘%%’ outputs a single ‘%’ |
(var) | Mapping variables (dictionary parameters) |
m.n. | m is the minimum total width of the display, n is the number of digits after the decimal point (if available) |
Beginning with Python 2.6, a new function, str.format(), was added to format strings, enhancing string formatting capabilities.
Python triple quotes
Python triple quotes allow a string to span multiple lines and contain line breaks, tabs, and other special characters. See the example below.
Example (Python 3.0+)
Triple quotes free programmers from the quagmire of quotes and special string literals, keeping a small string in a WYSIWYG (What You See Is What You Get) format.
A typical use case is when you need a block of HTML or SQL. String comprehension is very cumbersome when used in combination.
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 is added after Python 3.6. It is called a literal format string and is a new format string syntax.
Previously, we used to use the percent sign (%):
Example
>>> name = 'Geekdoc'<br>
>>> 'Hello %s' % name<br>
'Hello Geekdoc' <br>
The f-string format string starts with f and is followed by a string. The expression in the string is enclosed in curly braces {}. It will replace the calculated value of the variable or expression. The example is as follows:
Example
>>> name = 'Geekdoc'<br>
>>> f'Hello {name}' # Replace variable<br>
<br>
>>> f'{1+2}' # Use expression<br>
'3'<br>
<br>
>>> w = {'name': 'Geekdoc', 'url': 'www.geek-docs.com'}<br>
>>> f'{w["name"]}: {w["url"]}'<br>
'Geekdoc: www.geek-docs.com'<br>
This method is significantly simpler; you no longer need to decide whether to use %s or %d.
In Python 3.8, you can use the = symbol to concatenate expressions and results:
Example
>>> x = 1<br>
>>> print(f'{x+1}') # Python 3.6<br>
2<br>
<br>
>>> x = 1<br>
>>> print(f'{x+1=}') # Python 3.8<br>
'x+1=2'<br>
Unicode Strings
In Python 2, normal strings are stored as 8-bit ASCII codes, while Unicode strings are stored as 16-bit Unicode strings, which can represent a wider range of characters. The syntax is to prefix the string with u.
In Python 3, all strings are Unicode strings.
Python’s Built-in String Functions
Common Python string built-in functions are as follows:
Sequence Number | Method and Description |
---|---|
1 |
capitalize() |
2 |
center(width, fillchar) Returns a centered string of the specified width width. fillchar is the fill character, which defaults to a space. |
3 |
count(str, beg=0,end=len(string)) Returns the number of times str appears in string. If beg or end is specified, it returns the number of times str appears within the specified range. |
4 |
bytes.decode(encoding=”utf-8″, errors=”strict”) Python 3 does not have a decode method, but we can use the bytes object’s decode() method to decode a given bytes object. This bytes object can be decoded by str.encode(). Encode and return. |
5 |
encode(encoding=’UTF-8′,errors=’strict’) Encodes the string using the encoding specified by encoding. If an error occurs, a ValueError exception is raised by default, unless errors specifies ‘ignore’ or ‘replace’. |
6 |
endswith(suffix, beg=0, end=len(string)) |
7 |
expandtabs(tabsize=8) Converts tabs in string to spaces. The default number of spaces for tabs is 8. |
8 |
find(str, beg=0, end=len(string)) Checks whether str is contained within string. If beg and end are specified, checks whether the string is contained within the specified range. If so, returns the starting index; otherwise, returns -1. |
9 |
index(str, beg=0, end=len(string)) end=len(string)) Same as the find() method, except that an exception is raised if str is not in the string. |
10 |
isalnum() Returns True if the string contains at least one character and all characters are letters or digits. Otherwise, it returns False. |
11 |
isalpha() Returns True if the string contains at least one character and all characters are letters. Otherwise, it returns False. |
12 |
isdigit() Returns True if the string contains only digits. Otherwise, it returns False.. |
13 |
islower() Returns True if the string contains at least one case-sensitive character, and all of those characters are lowercase. Otherwise, returns False. |
14 |
isnumeric() Returns True if the string contains only numeric characters. Otherwise, returns False. |
15 |
isspace() Returns True if the string contains only whitespace. Otherwise, returns False. |
16 |
istitle() Returns True if the string is titleable (see title()), otherwise False. |
17 |
isupper() Returns True if the string contains at least one case-sensitive character, and all such characters are uppercase. Otherwise, False. |
18 |
join(seq) Join the string with the specified string as the delimiter. Combine all elements (string representations) in a string into a new string. |
19 |
len(string) Returns the length of the string. |
20 |
ljust(width[, fillchar]) Returns a new string with the original string left-aligned and padded to length width using fillchar. The default is spaces. |
21 |
lower() Convert all uppercase characters in a string to lowercase. |
22 |
lstrip() Truncate spaces or specified characters from the left side of a string. |
23 |
maketrans() Creates a character map conversion table. The simplest call accepts two arguments: the first is a string representing the characters to be converted, and the second is also a string representing the target. |
24 |
max(str) Returns the string str |
25 |
min(str) Returns the smallest letter in the string str. |
26 |
replace(old, new [, max]) Replaces str1 with str2. If max is specified, no more than max replacements are made. |
27 |
rfind(str, beg=0,end=len(string)) Similar to the find() function, but starts searching from the right. |
28 |
rindex(str, beg=0, end=len(string)) Similar to index(), but starts from the right. |
29 |
rjust(width,[, fillchar]) Returns a new string that is right-aligned and padded with fillchar (default is space) to a length of width. |
30 |
rstrip() Remove spaces from the end of string. |
31 |
split(str=””, num=string.count(str)) num=string.count(str)) |
32 |
splitlines([keepends]) Split by line (‘r’, ‘rn’, n’) and return a list containing each line. If keepends is False, line breaks are excluded; if True, line breaks are retained. |
33 |
startswith(substr, beg=0,end=len(string)) Checks if a string begins with the specified substring substr. Returns True if it does, otherwise returns False. If beg and end are not equal, return True. If a value is specified, it will be checked within the specified range. |
34 |
strip([chars]) Performs lstrip() and rstrip() on a string. |
35 |
swapcase() Converts uppercase to lowercase and lowercase to uppercase in a string. |
36 |
title() Returns a “title-ized” string, meaning all words begin with uppercase and all remaining letters are lowercase (see istitle()). |
37 |
translate(table, deletechars=””) Convert the characters in string according to the table given by str (containing 256 characters). |
38 |
upper() Convert lowercase letters in string to uppercase. |
39 |
zfill (width) Return a string of length width, right-aligned and filled with leading zeros. |
40 |
isdecimal() Checks whether a string contains only decimal characters. Returns true if true, otherwise returns false. false. |