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.
- 99.9% uptime and super-fast loading
- Advanced security features
- Domain and email included
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 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 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:
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. 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 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:
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 appropriate 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 especially 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 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:
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:
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:
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.
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 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:
text = "This is a sample text"
del text