avatarLiu Zuo Lin

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

  • Strings in Python can be defined using either single or double quotes.
  • Adding strings joins them together in the specified order.
  • Multiplying a string by an integer joins the string with itself that many times.
  • Compound assignment operators can be used to add or multiply a string with another string or an integer.
  • Basic indexing allows for the retrieval of a character at a specific index in a string.
  • Negative indexes can be used to count from the right side of the string.
  • Attempting to index a non-existent character will result in an index out of range error.

Basic Strings (Python From Zero To One — Part 18)

Day 88 of experimenting with video content

Intro to Strings

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.

String addition & multiplication

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.

Compound assignment operators

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

Basic indexing

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.

  • index of a in abcde is 0
  • index of b in abcde is 1
  • index of c in abcde is 2
  • index of d in abcde is 3
  • index of e in abcde is 4

Index out of range error

string = '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.

Negative indexes

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.

Conclusion

Hope this was clear and easy to understand.

Some Final words

If this story was helpful and you wish to show a little support, you could:

  1. Clap 50 times for this story
  2. Leave a comment telling me what you think
  3. Highlight the parts in this story that resonate with you

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/

If you enjoyed this article, consider trying out the AI service I recommend. It provides the same performance and functions to ChatGPT Plus(GPT-4) but more cost-effective, at just $6/month (Special offer for $1/month). Click here to try ZAI.chat.

Python
Python3
Python Programming
Programming
Coding
Recommended from ReadMedium