Strings are part of Python's sequence data type, as they are a collection of characters that have an ordered to them. Strings are immutable, thus, we cannot call expressions like
sample = 'P' and expect our new word to start with the letter
Strings in Python are declared by either single or double quotes. Strings are considered to be part of Python sequences, which is a data type consisting of data of a specific order.
>>> 'JohnDoe' JohnDoe >>> "JohnDoe" JohnDoe >>> "John's Dough" John's Dough >>> 'John's Dough' File "<stdin>", line 1 'John's Dough' ^
Note how using single quotes within single quotes gives an error.
If you want to escape a single quotation, use the backslash key (
\), or use double quotations.
>>> name = 'John\'sDoe' >>> print(name) John'sDoe >>> name = "John'sDoe" >>> print(name) John'sDoe
You may have noticed that we can output variable contents without the
print() function. The
print() function is preferred, however, since this omits the enclosing quotation marks and prints special characters.
>>> name = "John\tDoe" >>> name 'John\tDoe' >>> print(name) John Doe
Multiplying and adding strings is easy and straightforward. For example, if you want to repeat some string thrice, multiple by 3.
>>> 3 * 'har' 'harharhar' >>> 'hello' + ' ' + 'world' 'hello world'
Additionally, we may check for certain conditions between two strings with the
not in keywords.
>>> 'pp' in 'happiness' True >>> 'i' not in 'team' True
To compare two strings lexicographically, use the one of the following operators:
>>> 'hello' >= 'hi' False >>> 'aa' == 'AA' False >>> 'aaa' > 'a' True
To extract a substring of a string, use string indexing and point to posititions with indices. Like most other programming languages, Python has a zero-based index, meaning the first character starts at
>>> sample = 'QWERTY' >>> sample 'E' >>> sample 'Q' >>> sample Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range
Note the error when we try to access an index that is above the length of our string.
Negative indices may be used to count from the end of our string.
>>> sample = 'QWERTY' >>> sample[-2] 'T' >>> sample[-5] 'W'
We may also pull out entire substrings from a given string with given indices; this is called splicing. To specify a range, we point the index that it starts at, specify a colon (
:) then up to the index the substring ends at. Thus,
sample[m:n] would be from index
m up to but not including index
>>> sample[2:4] 'ER' >>> sample[2:-1] 'ERT' >>> sample[:5] 'QWERT' >>> sample[2:] 'ERTY' >>> sample[:] 'QWERTY'
Notice how we may omit one of the numbers, which will mean to go to either the beginning or end of that string. If we omit the
m beginning value, then it means to take from the beginning (prefix of a string). Furthermore, if we omit the ending
n value, we take it to the end (suffix of a string).
We may also add a third parameter, to specify a step. The following would select every other letter, starting from the zeroth and first index.
>>> sample[::2] 'QET' >>> sample[1::2] 'WRY'
To obtain the reverse of a string, simply use
[::-1]. This obtains the entire string, but steps backwards from the end.
>>> "abracadabra"[::-1] 'arbadacarba'
This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.$ Check price
Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on author Mark Lutz's popular training course, this updated fifth edition will help you quickly write efficient, high-quality code with Python. It's an ideal way to begin, whether you're new to programming or a professional developer versed in other languages.$ Check price