Python strings are one of the most important data types in the language. They are written in quotes and output using the print function. The strings cannot be changed af­ter­wards and can only be deleted as a whole.

Web hosting
The hosting your website deserves at an un­beat­able price
  • Loading 3x faster for happier customers
  • Rock-solid 99.99% uptime and advanced pro­tec­tion
  • Only at IONOS: up to 500 GB included

What is a Python String?

A Python string is basically a string con­sist­ing of a sequence of various in­di­vidu­al char­ac­ters. These types of strings exist in most internet pro­gram­ming languages and they are one of the most commonly used data types. You’ll most likely need to build and use Python strings in Python tutorials, Python “if else” loops, Python “while” loops, Python “for” loops, Python lists, or when learning about Python operators. Therefore, taking a closer look at the structure, function, and use of strings is def­in­itely worth­while.

Structure of a Python string

A Python string is written either in double or single quotes. The choice is up to you. A simple Python string looks like this: "example" or 'example'. Use the print function to il­lus­trate a Python string. It will look like this:

print("example")
print('example')

All char­ac­ters inside the quotation marks are a part of that Python string.

How do I assign variables?

Variables can be assigned to a Python string. This saves time and keeps your code clear which avoids some of the common Python problems. This is useful when it comes to longer strings. Here’s an example:

text = "This sample text is very long and therefore results in significantly longer code."
print(text)

Python strings in multiple lines

Three single or double quotes can be used if you want to assign a Python string in multiple lines. The line breaks will also be included in your output. You can see an example for this below:

text = """This sample text is very long and therefore results in significantly longer code.
It continues in a new line,
is extended by a third and fourth line
and ends with a full stop."""
print(text)

The output looks like this:

This sample text is very long and therefore results in significantly longer code.
It continues in a new line,
is extended by a third and fourth line
and ends with a full stop.

How do I output single char­ac­ters in Python string?

Square brackets are used to access a specific element of a Python string. In Python, an element is es­sen­tially every single character inside the quotation marks. Selecting a par­tic­u­lar element requires counting its position and naming it. Keep in mind that Python counts from 0. You will find an example of the code below:

text = "This sample text is very long and therefore results in significantly longer code."
print(text[3])

The program would output the fourth letter (0, 1, 2, 3) of the Python string in this case. That would be the `s´ from `this´.

Output Python strings with for loop

You can also read Python strings with a `for´ loop. A Python string can be used like an array in Python, so you can spell out any word with an ap­pro­pri­ate loop. This is the code:

for letter in "dandelion":
print(letter)

This is what the output looks like:

d
a
n
d
e
l
i
o
n

How do I determine the length of a Python string?

Use the len function to determine the length of a Python string. This is es­pe­cially useful for very long sections of code. The code would look like this using our example text:

text = "This sample text is very long and therefore results in significantly longer code."
print(len(text))

The output would be `81´ for all char­ac­ters including spaces and punc­tu­ation.

How do I check Python strings?

Use `in´ to check whether certain letters or terms are in a Python string. This will check if the term you’re looking for is present and will answer true or false. It will look like this:

text = "This sample text is very long and therefore results in significantly longer code."
print("therefore" in text)

The output in this case would be `true´.

With an `if´ query

Al­tern­at­ively, use an `if´ query to confirm that the term is in this section. Use the following code to do this:

text = "This sample text is very long and therefore results in significantly longer code."
if "therefore" in text:
print("Yes, 'therefore' is in this section.")

Since `therefore´ is in the code, the cor­res­pond­ing output looks like this:

Yes, 'therefore' is in this section.

Exclude oc­cur­rence

Use `not in´ to check if a term is not included. The two code examples il­lus­trate this. It works the same as the positive search queries.

text = "This sample text is very long and therefore results in significantly longer code."
print("short" not in text)
text = "This sample text is very long and therefore results in significantly longer code."
if "short" not in text:
print("No, 'short' is NOT in this section.")

How do I split Python strings?

It is also possible to split a Python string. This is a simple split example:

text = "This is a sample text"
print(text.split())

The output then results in:

["This", "is", "a", "sample", "text"]

Check out our Digital Guide to find out more about Python split.

How do I modify or delete Python strings?

A Python string cannot be changed af­ter­wards. It is also not possible to delete single char­ac­ters. The program will prevent any attempt to change it and display an error message. The only option to remove incorrect Python strings from the code is complete deletion. This is done using the del command. This works as follows:

text = "This is a sample text"
del text
Go to Main Menu