In this article on string methods, I’m going to show you a little bit about converting Python List To String. The methods that are in this group convert between a string and some composite data type. By either pasting objects together to make a string, or by breaking a string apart into its pieces.
These methods operate on or return iterables. interable is a general Python term for a sequential collection of objects. Iterating or walking through all the members of a collection is a common technique inside Python.
Many of these methods return either a list or a tuple which are very similar collections have ordered objects. But they have a couple of differences. A list is within square brackets and it’s mutable meaning that the contents can change. Whereas a tuple is within parentheses and is immutable. You can learn more about that by reading the Lists, Tuples, sets, and dictionaries article.
This is a very quick introduction to these topics, but you need to know a little about what they look like for this next set of methods and they’re examples.
Python List To String : The Join Method
The first method you’re going to try is dot join
str.join. which takes an iterable into it. It concatenates strings from that iterable. To start I’ll have you create a list.
A list is within square brackets. it will be a sequence of strings, make sure to open and close each string object with a single quote or double quote, and place a comma in between the objects.
Now you’ve created my list and you can check its type it is a class list how can you use the method join? well, join works with those iterables. in this case, you can give it a separator. in this case, maybe we’ll use a semicolon in a space that’s your separator, which is a string and since it’s a string, you can see all the methods that are there and join is one of them and you can see that we’ll concatenate any number of strings.
Here you can see the examples showing joining this list together. It will return it as a new string. Let’s try it with mylist, and see what returns. It’s a single string again join together using this separator. You could have used a comma and left it without a space even. One kind of interesting thing to think about is that any string is an iterable also. If you had a string of just a word and let’s say that word is lobster, and right now type is lobster, it’s a string. What if you were to join say using a colon word? it will take all the individual letters and it will iterate through and create a new string separating them.
One note: if you had a list and in this case, the list had a mix of types of things, let’s say an integer along with a handful of strings, again the type is definitely a list. What happens if you were to try using this as your separator and join my list? this one is saying that there is a problem with it there’s an exception a type error. it expected another string and found an integer instead. In this case, all three of these would have to string in order to do that join.
Well, you learn a method earlier that could fix that in its case you could say at integer of 23 and have it convert it into a string for us using STR. So nowhere to use Milas 3 and join it together and not end up with that type error.
Python Partition and Rpartion Methods
The next method is partition:
str.partition which takes a string of a separator. It divides a string based upon that separator. The returned value is the three-part tuple consisting of the portion preceding the separator, the separator itself, and the portion of the string following that separator.
Let’s say we had a string with egg and spam in a period separating the two. With Partition, it’s going to separate it into three parts using a specific separator. Let’s say our separator, in this case, is a period. it’s going to return three tuples containing the part before the separator, the separator itself, and the part after it. Let’s see what that looks like, and there you go again you can see the parentheses as opposed to these square brackets indicating this is a tuple and not a list.
If you were to have a longer string with these three words separated with dollar signs, how I’d partition work here? it is our new string. In this case, if you add the double dollar sign partition made, the partition-based upon the first occurrence of that string.
rpartition divides a string based upon a separator again. it functions exactly like partition, except that the string is split at the last occurrence instead of the first occurrence of the separator. So starting from the right like the other rmethods you’ve learned earlier. Rpartition do the same thing but it would work from the right side. If you were to take tea and partition it based on something that’s not within the string like let’s say a period. it would simply return the entire string and then an empty string followed by another empty string in your tuple.
Here’s your string “t” , see rpartition if you were to use a character that’s not in the string but do the reverse. Again partition works from the left and Rpartition working from the right.
Python List To String : The Split and Rsplit Method
Next is split
str.split it splits a string into a list of substrings. without any arguments, split will take your string and divide it into substrings delimited by any sequence of whitespace. It will return those substrings as a list. If there is a separator, it will be used for delimiting the split. The max split value by default is negative 1, which will mean it will split all the way across the entire string. If a value is inside the max split it will start from the left side and count up.
In this case, here’s a string with words separated by white space in this case just the space character. If you were to apply split to it as you can see here without a separator the default value is going to split based upon any whitespace and discard empty strings from the result. Let’s try a split just by itself and you can see it returns this list separating all the words based upon the white space between them. That white space could be tabs, newlines, or simply just plain old space.
If you have a string that uses a period as a separator, split in that case; if you were to enter in the value; will split based upon that. Using that delimiter. if you have something kind of unique with multiple periods; if you were to split this using that as the delimiter; it will return empty strings as part of your list. There would be a little string there, another string there. It’s still splitting based upon that delimiter just be aware if you have repeating characters that’s how it would behave.
Another quick note if you had a string had multiple characters of white space in between your words. In that case, even though these are repetitions. It will just take it all as one chunk of white space in between. Do one more example here, create a string name linked with AstateofDatat inside of it. And in this case, take that link and split it the delimiter is period sure but this time let’s put in the value for max split. Let’s say the maximum splits are only one. That would take it from the left side here WWW ASTATEOFDATA COM and separate the two out.
Rsplit works the same as split. The only thing that has changed is the max split value is counted from the right side. So try out your same link and this time tries rsplit with it. You can see that it shows almost the entire same information. Except for it’s going to split starting from the end the max split of 1. and there you can see the separation starting with the last period. In all other ways rsplit behaves the same as split. In the case of the default meaning max but is set to negative 1, both methods behave exactly the same.
Split lines will take a long string and break it based upon line boundaries. It will return them as a list ending the following characters or character sequences included in this table is considered to be a line boundary. It could be a new line, a carriage return, or any of these other escape sequences included inside here, with their Unicode or ASCII equivalent.
If the optional keep ends argument is set to true, it will include those line ending characters. If you have a large text input that’s been read in from a file; very often they’ll have new line escape sequences inside of it. It might look something like this:
You can see here at each line there’s a line break /n like a new variable thought moby split and take moby after splitting it into lines. Note the line breaks will not be included in the resulting list unless you set that optional argument to true. Basically you’re taking this text applying this method to it. Then putting that into this new variable mobysplit. So what does that look like?
Well, mobysplit is a list as you can see here. Each one of these text rings separated by a comma and the same way that you can access portions of a string you can access this list.
It’s pretty powerful what you can do with split lines if you have this large chunk of text. And how you could access it later as an iterable list instead. Now that you covered the majority of string methods. It’s time to talk about more advanced stuff starting in the next articles.
If you liked this article, please help me creating more of those by sharing it on social media. Feel free to follow me on twitter for your daily Python update. And check out my Python Course to Learn all the basics in just 3 hours!