Basic Strings (Python From Zero To One — Part 18)
Day 88 of experimenting with video content
Summary
This website article explains the basics of strings in Python, including their definition, addition and multiplication, compound assignment operators, basic indexing, negative indexes, and index out of range errors.
Abstract
The article, titled "Basic Strings (Python From Zero To One — Part 18)," covers the fundamental concepts of strings in Python. It explains how to define strings using single or double quotes, and demonstrates the use of addition and multiplication operators with strings. The article also introduces compound assignment operators, basic indexing, and negative indexes, and discusses the index out of range error that can occur when attempting to index a non-existent character. The article concludes by encouraging readers to engage with the content and provide feedback.
Opinions
Day 88 of experimenting with video content
a = 'hello world'
b = "apple pie"
We can think of strings as a sequence of characters, and we use either single or double quotes (both works) to define strings. Here, a
is a string, and b
is also a string.
a = 'apple'
b = 'pie'
print(a + b) # applepie
print(b + a) # pieapple
^ adding strings joins them together (order matters)
a = 'apple'
print(a * 3) # appleappleapple
^ multiplying a string with an integer n
joins n
of the string together.
Note — a string can only be added with another string, and a string can only be multiplied with an integer. We get errors if we try anything else.
a = 'apple'
a += 'pie'
print(a) # applepie
^ a += 'pie'
means we add 'pie'
to the back of a
a = 'apple'
a *= 3
print(a) # appleappleapple
^ a *= 3
means we multiply a
by 3
string = 'abcde'
print(string[0]) # a
print(string[1]) # b
print(string[2]) # c
print(string[3]) # d
print(string[4]) # e
When we index a string eg. string[n]
where n is an integer, we are actually getting the character at that certain index n
.
In Python, we start counting from 0, so the index of our first character a
would be 0.
a
in abcde
is 0b
in abcde
is 1c
in abcde
is 2d
in abcde
is 3e
in abcde
is 4string = 'abcde'
print(string[0]) # a
print(string[1]) # b
print(string[2]) # c
print(string[3]) # d
print(string[4]) # e
print(string[5]) # index out of range error
if we attempt to index a character that isn’t there, we get an index out of range error. Here in the string abcde
, we only have characters up til index 4 (5th position) — if we try to index 5, we just get an error.
string = 'abcde'
print(string[-1]) # e
print(string[-2]) # d
print(string[-3]) # c
print(string[-4]) # b
print(string[-5]) # a
When we pass in negative indexes when doing string indexing, we start counting from the right instead of the usual left side.
e
is at index -1
d
is at index -2
c
is at index -3
b
is at index -4
a
is at index -5
Which can be useful if we are dealing with characters nearer to the right end of our string.
string = 'abcde'
print(string[-1]) # e
print(string[-2]) # d
print(string[-3]) # c
print(string[-4]) # b
print(string[-5]) # a
print(string[-6]) # index out of range error
^ similarly, if we attempt to use a negative index that doesn’t exist in the string, we will just get an index out of range error.
Hope this was clear and easy to understand.
If this story was helpful and you wish to show a little support, you could:
These actions really really help me out, and are much appreciated!
Ebooks I’ve Written: https://zlliu.co/ebooks
LinkedIn: https://www.linkedin.com/in/zlliu/
Weird syntax but I’ve actually seen this in production code.
A docstring in Python is a string literal that appears right after the definition of a function, method, class, or module. Enclosed in…
#1 of 101-Awesome Python Guides by Tushar Aggarwal
Don’t get caught looking like a beginner! Learn these four important tips to impress your colleagues.