Python Strings Tutorial: Everything You Need to Know

Dr. Soumen Atta, Ph.D.
5 min readApr 6, 2023
Photo by Jexo on Unsplash

In Python, a string is a sequence of characters enclosed in quotes, either single quotes or double quotes. Strings are immutable in Python, which means that once they are created, their contents cannot be changed. However, you can create a new string by concatenating or slicing parts of the original string. In this tutorial, we’ll cover the basics of working with strings in Python.

Creating a string

To create a string, simply assign a value enclosed in quotes to a variable.

string_var = 'Hello, World!'

Accessing individual characters

You can access individual characters in a string using indexing. Indexing in Python starts from 0, so the first character in a string is at index 0, the second character is at index 1, and so on.

string_var = 'Hello, World!'
print(string_var[0]) # Output: H
print(string_var[7]) # Output: W

Slicing a string

--

--

Dr. Soumen Atta, Ph.D.

I am a Postdoctoral Researcher at the Faculty of IT, University of Jyväskylä, Finland. You can find more about me on my homepage: https://www.soumenatta.com/