# Python string

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 afterwards and can only be deleted as a whole.

## What is a Python String?

A Python string is basically a **string consisting of a sequence of various individual characters**. These types of strings exist in most [internet programming languages](https://www.ionos.com/digitalguide/websites/web-development/web-programming-languages/ "Web programming 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](https://www.ionos.com/digitalguide/websites/web-development/python-tutorial/ "Python tutorial"), [Python “if else” loops](https://www.ionos.com/digitalguide/websites/web-development/python-if-else/ "Python if else"), [Python “while” loops](https://www.ionos.com/digitalguide/websites/web-development/python-while-loop/ "Python while loop"), [Python “for” loops](https://www.ionos.com/digitalguide/websites/web-development/python-for-loop/ "Python for loop"), [Python lists](https://www.ionos.com/digitalguide/websites/web-development/python-list/ "Python list"), or when learning about [Python operators](https://www.ionos.com/digitalguide/websites/web-development/python-operators/ "Python operators"). Therefore, taking a closer look at the structure, function, and use of strings is definitely worthwhile.

## 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 illustrate a Python string. It will look like this:

```none
print("example")
print('example')
```

All characters 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](https://www.ionos.com/digitalguide/websites/web-development/troubleshooting-common-python-problems/ "Troubleshooting common Python problems"). This is useful when it comes to longer strings. Here’s an example:

```none
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:

```none
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 period."""
print(text)
```

The output looks like this:

```none
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 period.
```

## How do I output single characters in Python string?

Square braces are used to access a specific element of a Python string. In Python, an element is essentially every single character inside the quotation marks. Selecting a particular 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:

```none
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](https://www.ionos.com/digitalguide/websites/web-development/python-array/ "Python array"), so you can spell out any word with an appropriate loop. This is the code:

```none
for letter in "dandelion":
print(letter)
```

This is what the output looks like:

```none
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 especially useful for very long sections of code. The code would look like this using our example text:

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

The output would be “81” for all characters including spaces and punctuation.

## 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:

```none
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

Alternatively, use an “if” query to confirm that the term is in this section. Use the following code to do this:

```none
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 corresponding output looks like this:

```none
Yes, 'therefore' is in this section.
```

### Exclude occurrence

Use “not in” to check if a term is not included. The two code examples illustrate this. It works the same as the positive search queries.

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

```none
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:

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

The output then results in:

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

Check out our Digital Guide to find out more about [Python split](https://www.ionos.com/digitalguide/websites/web-development/python-split/).

## How do I modify or delete Python strings?

A Python string cannot be changed afterwards. It is also not possible to delete single characters. 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:

```none
text = "This is a sample text"
del text
```


This is a markdown version of: [https://www.ionos.com/digitalguide/websites/web-development/python-string/](https://www.ionos.com/digitalguide/websites/web-development/python-string/) for AI/LLM consumption.