There are certain things in life: death, tax bills, and programmers who have to deal with strings. Strings can take many forms. They can be unstructured text, user names, product descriptions, database list names, or anything else we describe in our language.

Since string data is almost ubiquitous, it is important to know how the tools you use do string manipulation. Fortunately, the Python language’s manipulation of strings is very simple compared to other languages and even older versions of Python.

You’ll learn some of the most basic string operations in this article: splitting, concatenating, and concatenating. You will not only learn how to use these tools, but also gain insight into how they work.


String splitting

In Python, strings are represented as STR objects and are immutable: this means that you cannot change objects in memory directly. Keeping these two features in mind will help you learn (and remember) how to use.split ().

Can you guess how these two characteristics of strings relate to the splitting functionality in Python? If you guessed that.split() is an instance method because strings are a special type, you’re right! In some other languages, such as Perl, the raw string is used as input to a separate.split() function rather than calling the string itself with a method.

Note: How do I call string methods

The.split () notation shown here is the method called on the string instance. Call it a static method, but this is not ideal because it is “verbose.” For completeness, here’s an example:

It’s a little clunky when compared to the preferred method below:

For more on instances, classes, and static methods in the Python language, see our

Python tutorial

What if strings are immutable? This is a reminder that string methods do not support in-place operations, but they return a new object in memory.

Note: Operate in place

An in-place operation is an operation that directly changes the object being invoked. A common example is the.append() method on a list: When you use the.append() function on a list, the original list is changed to include.append() input.

No argument string splitting

Before diving into string splitting, let’s take a look at a simple example:

Python code:

Output result:

This is actually a special case of the.split() call, so I chose it for simplicity. If no separator is specified,.split() uses a space as the separator.

Another feature of this no-arguments.split() call is that it automatically removes leading, trailing, and consecutive whitespace from the string. Compare the result of calling split() on the following string without the separator with “”(space) as the separator:

Python code:

Results:

Python code:

Results:

The first thing to notice is that this example reflects the immutable nature of strings in Python: subsequent calls to.split() operate on the original string, not the list generated after the first call to.split().

The second thing you should notice is that a no-argument call to.split() extracts the words in the sentence and dismisses any Spaces.

Specifying delimiters

The.split(” “) result is straightforward. When there are leading or trailing delimiters, you get an empty string, as you can see in the first and last elements of the result list.

When there are multiple consecutive delimiters (such as multiple Spaces between “this” and “is” and between “is” and “my”), the first delimiter will be used as the delimiter, and subsequent delimiters will enter the result list as empty strings.

Note: the separator when calling.split ()

While the above example uses a single space character as a.split() delimiter, there is no restriction on the type or string length of the delimiter you use. The only requirement is that your delimiter be a string. You can use information from “…” To “separator” or anything else.

Use Maxsplit to qualify the split

.split() has an optional argument called maxsplit. By default,.split() will do all possible splits when called. However, if maxsplit is assigned, only a specified number of splits are generated. Using our previous example string, we can see maxsplit in action:

Python code:

Running results:

As shown above, if maxsplit is set to 1, the first blank area is used as the delimiter and the rest is ignored. Let’s do some exercises to test everything we’ve learned so far.

Exercise: “Try it for yourself: Maxsplit”

What happens if maxsplit is assigned a negative number?

Answer: “Try it for yourself: Maxsplit”

.split() splits the string on all separators, which is the same as the default if Maxsplit is not set.

Exercise: “Chapter Comprehension Check”

You recently got a comma-separated (CSV) file, but it was badly formatted. Your task is to extract each row into a list where each element represents the column of the file. What’s wrong with the format of this file? The “Address” field contains multiple commas, but actually needs to be represented as a single element in the list!

Suppose the file read into memory is a multi-segment string like the following:

The CSV file:

The desired output should be a list of lists like this

Python output:


The inner list is the column element of the CSV file we care about, each inner list is a row of the CSV file, and the outer list groups all the rows together.

Answer: “Chapter comprehension check”

Here’s my answer. The results can be obtained in several ways. The important thing is how to use.split(), and use additional arguments to get the desired result:

Python code:

We called.split() twice. The first call may be a little hard to understand, but don’t worry! As we walk through these expressions, you will become familiar with them. Take a look at the first.split() call: unsplit.split(” n”)[1:].

The first element is unsplit, which is a variable pointing to the input string. And then.split() calls.split(” n”). Here, we use a special character, the newline character, as the separator.

What does it do? As the name implies, it indicates that when reading the contents of a string, every character after it should appear on the next line. Multi-line strings like the one we used, input_string, have a hidden n at the end of each line.

The last part of this line may be a bit new: [1:]. This statement outputs a new list before reaching this section. [1:] looks like a list index representation, but it is actually a list index representation! The extra index representation at the end of the line outputs us a slice of the list. In this case, we fetch index 1 and everything after it, discarding the element at index 0.

In summary, we iterate over a list of strings where each element represents every line in the multi-line input string except the first.

For each line of string, we use.split() again for character splitting, but this time we use maxsplit to split only the first two commas, leaving the address part intact. We then add the result to the defined list array and return it to the caller.

Concatenation and concatenation of strings

Another basic operation on strings is the opposite of splitting strings: string concatenation. If you haven’t paid attention to this feature, don’t worry. This is just a fancy way of “sticking” strings together.

Use the + operator to connect

There are several ways to do this, depending on what you are trying to achieve. The simplest and most common method is to add multiple strings together using the plus sign (+). Simply place a + between the strings you want to concatenate:

Pyton code:

Results:

As with math, you can also multiply strings to do string repetition:

Pyton code:

Results:

Remember, the string itself is immutable! If you want to store string concatenation or duplicate results in a variable, you must assign the result to a new string variable to save.

Pyton code:

Results:

Pyton code:

Results:

Pyton code:

Results:

If it is not an immutable string, full_sentence will print “Hello, world, world”.

Another thing to note is that the Python language does not do implicit string conversions. If you try to concatenate a string with a non-string type, Python raises TypeError:

Pyton code:

Results:

The reason for the error is that in Python, you can only concatenate strings with other strings, which is a feature of the Python language that you need to be aware of. If you’ve ever worked with a language like JavaScript, you know that implicit type conversions are attempted.