Python strings are among the most important data types in the language. They are written in quotes and output via the print function. Strings cannot be changed afterwards and can only be deleted as a whole.
Web hosting with personal advisor!
Powerful, flexible and high-performance web hosting with mailbox, personal advisor and domain included 1time year !
What is a Python string?
To summarize, a Python string is a string composed of the sequence of different individual characters. Such strings are found in most Internet programming languages; they belong to the data types that are probably the most used overall. Whether you’re looking at a Python tutorial to learn about Python if-else loops, Python while loops, Python for loops, Python lists, or using Python operators: you’ll probably need to construct and use Python strings for this purpose. It is therefore useful to consider the structure, function and use of these character strings.
Structure of a Python string
A Python string is written in double or single quotes. Different ratings make no difference. A simple Python string therefore looks like this: « example » or ‘example’. To display a Python string, use the print function. It looks like this:
print ( "exemple" )
print ( 'exemple' )
All characters in quotes are part of that particular Python string.
Assign variables
You can assign a variable to a Python string. This not only saves time, especially with longer strings, but also keeps your code clear and avoids some problems in Python. Here is an example:
x = "Cet exemple de texte est très long et donne donc lieu à un code beaucoup plus long."
print ( x )
Python strings on multiple lines
If you want to form and arrange a Python string over multiple lines, you can use three single or double quotes for this purpose. Line breaks are then also taken into account in your output. Here is an example :
x = """Cet exemple de texte est très long et donne donc lieu à un code beaucoup plus long.
Il se poursuit dans une nouvelle ligne,
est élargi à une troisième et quatrième ligne
et se termine enfin par un point.""""
print ( x )
The corresponding output then looks like this:
Cet exemple de texte est très long et donne donc lieu à un code beaucoup plus long.
Il se poursuit dans une nouvelle ligne,
est élargi à une troisième et quatrième ligne
et se termine enfin par un point.""""
Output individual characters in the Python string
To access a specific element of a Python string, use square brackets. Element in Python ultimately matches any single character within quotes. To select a particular item, simply count and name its position. Keep in mind, however, that the count starts from 0 in Python. Here is an example of the corresponding code:
x = "Cet exemple de texte est très long et donne donc lieu à un code beaucoup plus long."
print ( x [ 2 ] )
In this case, the program outputs the third letter (0, 1, 2) from the Python string. This would be the “t” of “Cet”.
Output Python strings with the for loop
You can also read Python strings with a for loop. A Python string is used like an array in Python so you can spell any word with a matching loop, eg. Here is the code:
for x in "pissenlit":
print ( x )
The output looks like this:
Determine the length of a string in Python
To determine the length of a string in Python, use the len function. This is especially useful for very long sections of code. For our sample text, the code looks like this:
x = "Cet exemple de texte est très long et donne donc lieu à un code beaucoup plus long."
print ( len ( x ) )
In this case, the result is « 83 » for all characters, including spaces and punctuation.
Check Python strings
To check if certain letters or terms are contained in a Python string, use « in ». This checks if the search term exists and responds with true or false. It looks like this:
x = "Cet exemple de texte est très long et donne donc lieu à un code beaucoup plus long."
print ( "donc" in x )
In this case, the output would be a simple « true ».
With an if query
You can also use an if statement to confirm the use of a search term. Use the following code for this purpose:
x = "Cet exemple de texte est très long et donne donc lieu à un code beaucoup plus long."
if "donc" in x:
print ( "Oui, cet extrait contient le terme 'donc'." )
Since « therefore » is present in the code, the corresponding output looks like this:
Étant donné que « donc » est présent dans le code, la sortie correspondante se présente comme suit :
Exclude occurrences
To ensure that a term is not included, use the phrase “not in”. Here are the two code samples. The functionality is the same as that of positive search queries.
x = "Cet exemple de texte est très long et donne donc lieu à un code beaucoup plus long."
print ( "court" not in x )
x = "Cet exemple de texte est très long et donne donc lieu à un code beaucoup plus long."
if "court" not in x:
print ( "Non, cet extrait NE contient PAS le terme 'court'." )
Split Python strings
It is also possible to subdivide a Python string. A simple example of division looks like this:
text = "Voici un exemple de texte"
print ( text.split ( ) )
The output then shows this:
[ "Voici", "un", "exemple", "de", "texte" ]
More information on Python split can be found in the IONOS Digital Guide.
Edit or remove Python strings
A Python string cannot be modified later. Even individual characters cannot be deleted. The program will therefore prevent any modification attempt and return an error message. So the only option to remove erroneous Python strings from the code is complete removal. This is done with the del command. This works as follows:
text = "Voici un exemple de texte"
del text