String Type Fundamentals
# let us assign the string "Python 3.X" to the variable S
>>> S = "Python 3.X"
# check the length of S
>>> len(S)
10
# access the first unitary string in the sequence behind S
>>> S[0]
"P"
# access the last unitary string in the sequence behind S
>>> S[len(S)-1]
"X"
# or, equivalently
>>> S[-1]
"X"
# access the i-th, e.g., 3rd, unitary string in the sequence behind S
>>> S[3]
"h"
# access the unitary strings between the i-th and j-th positions in the
# sequence behind S
>>> S[2:5]
"tho"
# access the unitary strings following the i-th position in the sequence
# behind S
>>> S[-3:]
"3.X"| Literal/operation | Interpretation |
|---|---|
S = "" |
Empty string |
S = '' |
Single quotes, same as double quotes |
S = "spam's" |
Single quote as a string |
S = 'spam\'s' |
Escape symbol |
length(S) |
Length |
S[i] |
Index |
S[i:j] |
Slice |
S1 + S2 |
Concatenate |
S * 3 |
Repeat S n times (e.g., three times) |
"text".join(strlist) |
Join multiple strings on a character (e.g., “text”) |
"{}".format() |
String formatting expression |
S.strip() |
Remove white spaces |
S.replace("pa", "xx") |
Replacement |
S.split(",") |
Split on a character (e.g., “,”) |
S.lower() |
Case conversion — to lower case |
S.upper() |
Case conversion — to upper case |
S.find("text") |
Search substring (e.g., “text”) |
S.isdigit() |
Test if the string is a digit |
S.endswith("spam") |
End test |
S.startswith("spam") |
Start test |
S = """...multiline...""" |
Triple-quoted block strings |
# let us assign S1 and S2 to two strings
>>> S1 = "Python 3.X"
>>> S2 = "Julia"
# check the length of S1 and S2
>>> len(S1)
10
>>> len(S2)
5
# display the S1 repeated five times
>>> S1 * 5
"Python 3.XPython 3.XPython 3.XPython 3.XPython 3.X"
# display the concatenation of S1 and S2
>>> S1 + S2
"Python 3.XJulia"
# display the concatenation of S1, whitespace, and S2
>>> S1 + " " + S2
"Python 3.X Julia"
# display the outcome of joining S1 and S2 with a whitespace
>>> " ".join([S1, S2])
"Python 3.X Julia"
# display the outcome of joining S1 and S2 with an arbitrary string object
>>> " Vs. ".join([S1, S2])
"Python 3.X Vs. Julia"
# string formatting
>>> "Both {} and {} have outstanding ML modules".format(S1, S2)
"Both Python 3.X and Julia have outstanding ML modules"# let us assign S to a string object
>>> S = "Both Python 3.X and Julia have outstanding ML modules"
# strip target leading characters
>>> S.lstrip("Both ")
"Python 3.X and Julia have outstanding ML modules"
# careful: lstrip takes a SET of characters, not a prefix
>>> "tooth".lstrip("Both ")
""
# to remove a prefix, say so
>>> S.removeprefix("Both ")
"Python 3.X and Julia have outstanding ML modules"
# replace target characters
>>> S.replace("Python 3.X", "R")
"Both R and Julia have outstanding ML modules"
# split string on target characters
>>> S.split(" and ")
["Both Python 3.X", "Julia have outstanding ML modules"]
# make the string lower case
>>> S.lower()
"both python 3.x and julia have outstanding ml modules"# let us assign S to a string object
>>> S = "The first version of Python was released in 1991"
# search for "Python" in S
>>> S.find("Python")
21
# search for "Julia" in S
>>> S.find("Julia")
-1
# slice the string the get Python's release year information
>>> SS = S[-4:]
# display SS
>>> SS
"1991"
# test if all characters in SS are digits
>>> SS.isdigit()
True
# test if S ends with "1991" / SS
>>> S.endswith(SS)
True# single-line print
>>> print("Hello world!")
Hello world!
# multi-line print
>>> print(
... """
... =======================================================
... COL A | COL B | ... | COL K
... -------------------------------------------------------
... Sheldon | Cooper | ... | bazinga.com
... -------------------------------------------------------
... NOTES: this table has fake data
... """
... )
=======================================================
COL A | COL B | ... | COL K
-------------------------------------------------------
Sheldon | Cooper | ... | bazinga.com
-------------------------------------------------------
NOTES: this table has fake data