We have the answers to your questions! - Don't miss our next open house about the data universe!

Python strings: everything you need to know

- Reading Time: 4 minutes
Python strings: everything you need to know

Strings are a fundamental type of programming. They are a sequence of alphabetic, numeric or symbolic characters. Python strings are frequently used when processing text, structured data or data from external sources.

So, if you want to start programming in Python, you need to know how to handle them. Strings are immutable, which means that once created, they cannot be modified. They can, however, be manipulated using various methods. Let’s take a look at these features.  

What is Python string?

A Python string can be defined in different ways: single or double quotation marks, but also triple quotation marks. Using three single or three double quotation marks allows line breaks to be taken into account when displaying the string.

 

💡Related articles:

Matplotlib: Master Data Visualization in Python
Python Crash Course: Get started
Mastering Machine Learning in Python: Data-Driven Success
Python Programming for Beginners – Episode 3
Django: All about the Python web development framework

 

 

You can assign a Python string to a variable to optimize your code and avoid overloading it with long strings that would repeat throughout the code. Simply display a Python string using the print() function.

Warning: if you wish to display a string containing single or double quotation marks, a SyntaxError will be returned.

To overcome this problem, triple quotation marks should be used when defining the string, or escape sequences. These are introduced by a backslash and are very useful for displaying these special characters; we’ll come back to them in more detail in a later section.

How do I index with the string function?

It’s important to remember that a Python string works like a list. You can therefore access a particular character in the string using square brackets, as in the example below. As with lists, you can index a Python string with negative numbers. Thus, x[-1] returns the last element of the string. Remember that indexing in Python starts at 0.

To return a certain subsequence of a Python string, we use slicing. The command x[start:end] returns the subsequence of characters starting at index start up to, but not including, index end.

Associated methods

A large number of different methods can be performed on Python strings.

len(x) Returns the length of the string.
‘Hello’ in x Tests whether a character or string is in x. It returns a boolean, True if ‘Hello’ is in x or False otherwise.
x.isalpha() Returns True if the Python string consists of alphabetic characters only.
x.isdecimal() Returns True if x consists of digits only. Note that this method does not detect negative integers and non-integer decimal numbers.
x.startswith(“start”) / x.endswith(“start”) Tests whether the string starts (resp. ends) with “start”.
x.find(“test”) Searches for the string “test” in the string x and returns the index where it starts or -1 if it's not found in the string.
x.count(sub, beg, end) Counts the number of occurrences of sub in x between beg and end.

Operations on Python strings

If you can’t modify a character string, there are several operations that allow you to create new strings from existing ones.

x.replace(“old”, “new”) Returns the string x where all occurrences of “old” have been replaced by “new”.
x.split( ) Splits a Python string based on the character passed as an argument, here a space. This operation returns a list of the divided elements.
x + y Concatenates the two strings x and y, thus returning a string composed of string x followed by y.
x*2 Creates a new string composed of the repetition of the string x twice.
capitalize(x) Capitalizes the first character of x.
x.lower() / x.upper() Converts x to lowercase letters and vice versa.
max(x) / min(x) Returns the character in x closest to the beginning of the alphabet and vice versa.
x[::-1] Reverses the Python string.

Delete

As mentioned earlier, Python strings are immutable. Once created, they cannot be modified, nor can a certain character be deleted, for example. As in the example below, if you wish to replace the first character of x, it will return an error.

If your string is incorrect, the only option is to delete it. This is done with the del command.

Escape characters

As mentioned above, escape or escape sequence characters are introduced by a backslash. They are used to insert special characters such as quotation marks, or to perform specific operations. For example, you can use “\n” for linefeeds, “\t” for tabulations, or “\” to display a backslash.

Formatting

Formatting Python strings corresponds to formatting methods. The concatenation method described above is one form of formatting.

However, it is not the most useful method, as it can only be used to assemble data of the same type. 

So, if you want to concatenate a string with numeric variables, you’ll need to convert the numeric variables to Python string using the str() command.

The format method is a much simpler way of formatting Python strings. Strings placed as arguments to the format() method will be replaced, in order, where braces appear. Unlike concatenation, this method supports different types of variables. Within the braces, you can specify the index of the argument to be used, or the name of the variable to be used among the arguments.

Python version 3.6 introduces a new feature: f-strings. This method offers better readability and performance than the format method.

To use this method, insert the letter “f” before the definition of a Python string variable. As with the format method, you can then add variables of different types to the string using braces.

This also makes it possible to use the various methods associated with Python strings explained above inside the braces. If you use f strings, you’ll need to be careful to define the variables called beforehand; this problem doesn’t arise if you use the format method.

In the past, formatting was done using the % command, so you can still find this type of formatting, even if this method is now obsolete.

Conclusion

Python strings are widely used in Python programming, particularly for manipulating text data, displaying results and word processing. You now have a good grasp of this type and its subtleties.

To learn how to master Python, DataScientest training courses offer a “learning by doing” approach that will turn you into an expert in programming and data science, graduating you as a Data Scientist, Data Analyst or Data Engineer.

You are not available?

Leave us your e-mail, so that we can send you your new articles when they are published!
icon newsletter

DataNews

Get monthly insider insights from experts directly in your mailbox