Enchanted Code

Python 3 strings tutorial

3 minutes read

Intro

This tutorial will teach you about the string data-type in Python.

Requirements

  • Know basics of Python 3 if not read here
  • Knowledge of Computer Science data-structures will help

What are they?

A string data-type in Python allows us to store a sequence of characters. Strings are a immutable type meaning they can’t be modified after it has been defined. We can however replace the whole string with another one. Strings in Python are also indexed, which allows us to access them as if they were a list.

Every index of a string is one character.

Unlike other programming languages there is no “char” type to store a singular character.

Modifying a strings data requires the reference the object to be redefined.

Creation

Defining a String in python is quite simple, you just need to wrap what you want as a string into double quotes.

1
2
my_first_str = "Hello World!"
print(my_first_str)

When the code is run you should get Hello World! output to your terminal.

Accessing

When we want to access a String’s content, there are several ways we can achieve this. We can get all content, a single index (character), or we can use what is “slicing”.

Accessing the whole content was shown when creating the String in the previous example, when we output the string to the console.

Indexed

Accessing a single character by index is the same as when using a list.

1
2
w_char = my_first_str[6]
print(w_char)

When this code is run you should see the character W output.

Remember indexes in Python start at 0

A white-space counts as a character and therefore has an index

Slicing

We can use the same slice operation from the list data-structure.

1
2
hello = my_first_str[:5]
print(hello)

When this code is run you should see the word Hello output.

When slicing [0:x] and [:x] mean the same thing

Modifying

Remember we can’t modify a string’s data directly, as it must be redefined. So changing a character through it’s index or appending/inserting methods will not work.

We can however use concatenation. Which was covered in a previous tutorial here.

1
2
my_first_str = my_first_str[:6] + "Leo"
print(my_first_str)

In this code example I am using slicing the select the Hello part. Then it is concatenating the word Leo, then storing it back into the named variable “my_first_str”. When run you should see the following output: Hello Leo.

Extras

Like most other data-types in Python, strings have lot’s of extra features that can be used. I will show you some of them.

Code Description
.upper() Makes a string uppercase
.lower() Makes a string lowercase
.reversed() Reverses the string backward

There is also special characters you can place in a string. You can achieve this using the backslash character, because of this if you want to use the backslash as a character you will have to place multiple.

Here is some of the notations you can use:

Notation Description
\\ Backslash character
\' Single quote character
\" Double quote character
\n Newline
\t Horizontal tab
\v Vertical tab

End

There are many more things you can do with strings. One of them is string formatting, you can read about that here.

See Also

Buy Me A Coffee