# 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.co.uk/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.co.uk/digitalguide/websites/web-development/python-tutorial/ "Python tutorial"), [Python “if else” loops](https://www.ionos.co.uk/digitalguide/websites/web-development/python-if-else/ "Python if else"), [Python “while” loops](https://www.ionos.co.uk/digitalguide/websites/web-development/python-while-loop/ "Python while loop"), [Python “for” loops](https://www.ionos.co.uk/digitalguide/websites/web-development/python-for-loop/ "Python for loop"), [Python lists](https://www.ionos.co.uk/digitalguide/websites/web-development/python-list/ "Python list"), or when learning about [Python operators](https://www.ionos.co.uk/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.co.uk/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 full stop."""
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 full stop.
```

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

Square brackets 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.co.uk/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´.

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


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