Strings can be used to represent just about anything that can be encoded as text or bytes. contents of text files loaded into memory, Internet addresses,Python source code, and so on. Strings can also be used to hold the raw bytes used for media files and network transfers, and both the encoded and decoded forms of non-ASCII Unicode text used in internationalized programs. Python strings are categorized as immutable sequences, meaning that the characters they contain have a left-to-right positional order and that they cannot be changed in place.
Here are some examples of strings in Python:
Empty strings L1T = ''
Double quotes, same as single L1T = "spam's"
Escape sequences L1T = 'Lev\el\1\Tech'
Triple-quoted block strings L1T = """...multiline..."""
Raw strings (no escapes) L1T = r'\usr\bin\Python-3.5'
Byte strings in 2.6, 2.7, and 3.X L1T = b'sp\xc4m'
Concatenate, repeat L1 + L2
and many more variety of ways to code strings for processing, strings support expression operations such slicing (extracting sections), indexing (fetching by offset), which we will eventually cover in other EP's. Beyond the core set of string tools above, Python also supports more advanced pattern-based string processing with the standard library’s re
(for “regular expression”) module and even higher-level text processing tools such as XML parsers which I hope to discuss in later Ep's. This Ep's is about fundamentals.
Q: So I see the examples you posted above but I'm a Noob so can you explain?
A: Here we go !
string literals can be written enclosed in either two single or two double quotes the two forms work the same and return the same type of object. The reason for supporting both is that it allows you to embed a quote character of the other variety inside a string without escaping it with a backslash. You can use Idle 3 to practice:
>>> 'Level1Tech', "Level1Tech"
('Level1Tech', 'Level1Tech')
We will generally use single quotes around strings just because they are marginally easier to read, except in cases where a single quote is embedded in the string. This is a purely subjective style choice, but Python displays strings this way too and most Python programmers do the same today, so you probably should too. here is an example :
>>> forum = 'Level' + '1' + 'Tech'
>>> forum
'Level1Tech'
>>>
without the +
operator between them Python will invoke concatenation explicitly. Adding commas between these strings would result in a tuple, not a string. Also notice in all of these outputs that Python prints strings in single quotes unless they embed one. If needed, you can also embed quote characters by escaping them with backslashes like in the example below:
>>> 'The Knight\'s of Python'
"The Knight's of Python"
>>>
Which leads us into Escape sequences. Backslashes are used to introduce special character codings known as escape sequences, which let us embed characters in strings that cannot easily be typed on a
keyboard. The character \
, and one or more characters following it in the string literal, are replaced with a single character in the resulting string object, which has the binary value specified by the escape sequence. See the example below:
>>> L1t = 'Level\n1\tTech'
>>> print(L1t)
Level
1 Tech
>>>
The two characters \n
stand for a single character—the binary value of the newline character in your character set, Similarly, the sequence \t
is replaced with the tab character. To be completely sure how many actual characters are in this string, use the built-inlen
function it returns the actual number of characters in a string, regardless of how it is coded or displayed.
>>> len(L1t)
12
>>>
Python recognizes a full set of escape code sequences as shown below:
Escape Meaning
\ newline Ignored (continuation line)
\\ Backslash (stores one \ )
\' Single quote (stores ' )
\" Double quote (stores " )
\a Bell
\b Backspace
\f Formfeed
\n Newline (linefeed)
\r Carriage return
\t Horizontal tab
\v Vertical tab
Q: Very cool ! I should take a moment to practice these...
A: The next Ep will be a more broader explanation of strings so practice these to understand them better.
Code On Code_Warriors !