- Python split string into list of characters (4 Ways)
- Method 1- Python Split String into list of characters using list()
- Method 2- Python Split String into list of characters using For loop and append()
- Method 3- Split String into list of characters using list Comprehension
- Method 4- Split String into list of characters using map and lambda
- Conclusion
- Split all characters string python
- # Table of Contents
- # Split a String into a List of Characters in Python
- # Split a String into a list of characters using a list comprehension
- # Split a String into a list of Characters using a for loop
- # Split a String into a List of Characters using iterable unpacking
- # Split a String into a List of Characters using extend
- # Split a String into a List of Characters using map()
- # Additional Resources
- Python Split String By Character – Split String Using split() method
- Python Split String By Character Tutorial – Getting Started With Examples
- What Is split() function ?
- Splitting String By Whitespace
- Splitting String By Colon ‘ : ‘
- Splitting String By Hash Tag ‘ # ‘
- Splitting String By Comma ‘ , ‘
- Splitting String By Position
- Splitting String By A Particular Characters
- Splitting String By Special Characters
- Splitting String By Maxsplit
- Recommended Posts
Python split string into list of characters (4 Ways)
In this article, we will be solving a program of python split string into list of characters. We will be discussing 4 methods to do this particular task.
Method 1- Python Split String into list of characters using list()
In Python, the easiest way to convert the string in list of characters is by using a python inbuilt function list(). By using list() the string is converted into a list that contains all the characters of the given string.
list() – This method returns a list in Python.
Syntax: list(iterable_object)
The iterable object can be collection (set, dictionary) or it could be a sequence like string or tuple.
Python Program:
#input string input_string = "ProblemSeeker" # list() is used to split string into list of charcters output_list = list(input_string) print("The input string is:",input_string) print("The output list of characters is:") print(output_list)
The input string is: ProblemSeeker The output list of characters is: ['P', 'r', 'o', 'b', 'l', 'e', 'm', 'S', 'e', 'e', 'k', 'e', 'r']
Method 2- Python Split String into list of characters using For loop and append()
In this method, we will be using a for loop to iterate within the characters for of string and then add character by character to the list. To add the character or any element in a list we use the append method.
append() method: To add an element at the end of the list we use the append() method.
Here the element can be any data type (string, float, int, list, etc)
Python Program
#input string input_string = "ProblemSeeker" #initialization of output list output_list = [] for character in input_string: #for loop #adding each character to list using append function output_list.append(character) print("The input string is :",input_string) print("The output list of characters is:") print(output_list)
The input string is : ProblemSeeker The output list of characters is: ['P', 'r', 'o', 'b', 'l', 'e', 'm', 'S', 'e', 'e', 'k', 'e', 'r']
Method 3- Split String into list of characters using list Comprehension
This method is similar to the for loop method but it is shortened and also we don’t use the append() method here as we are using list comprehension.
To know about the list comprehension you can read about it from here.
Python Program
#input string input_string = "ProblemSeeker" #using list comprehension output_string = [character for character in input_string] print("The input string is :",input_string) print("The output list of charcters is:") print(output_list)
The input string is : ProblemSeeker The output list of characters is: ['P', 'r', 'o', 'b', 'l', 'e', 'm', 'S', 'e', 'e', 'k', 'e', 'r']
Method 4- Split String into list of characters using map and lambda
Another way to do this particular task is by using the map and lambda function. To get the concept of map and lambda you can read it from here.
Python Program
#input string input_string = "ProblemSeeker" #using map and lambda function output_list = list(map(lambda character: character, input_string)) print("The input string is :",input_string) print("The output list of charcters is:") print(output_list)
The input string is : ProblemSeeker The output list of characters is: ['P', 'r', 'o', 'b', 'l', 'e', 'm', 'S', 'e', 'e', 'k', 'e', 'r'
Conclusion
Hence by using For loop, list() method, list comprehension, and map and lambda we can split string into list of characters in Python.
I am Passionate Computer Engineer. Writing articles about programming problems and concepts allows me to follow my passion for programming and helping others.
Split all characters string python
Last updated: Feb 19, 2023
Reading time · 4 min
# Table of Contents
# Split a String into a List of Characters in Python
Use the list() class to split a string into a list of characters, e.g. my_list = list(my_str) .
The list() class will convert the string into a list of characters.
Copied!my_str = 'bobby' my_list = list(my_str) # 👇️ ['b', 'o', 'b', 'b', 'y'] print(my_list)
The list class takes an iterable and returns a list object.
When a string is passed to the class, it splits the string on each character and returns a list containing the characters.
# Split a String into a list of characters using a list comprehension
Copied!my_str = 'bobby' my_list = [letter for letter in my_str] # 👇️ ['b', 'o', 'b', 'b', 'y'] print(my_list)
List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.
You can also filter letters out of the final list when using this approach.
Copied!my_str = 'b o b b y' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['b', 'o', 'b', 'b', 'y'] print(my_list)
The string in the example has spaces.
Instead of getting list items that contain a space, we call the strip() method on each letter and see if the result is truthy.
The str.strip method returns a copy of the string with the leading and trailing whitespace removed.
If the string stores a space, it would get excluded from the final list.
# Split a String into a list of Characters using a for loop
You can also use a simple for loop to split a string into a list of characters.
Copied!my_str = 'bobby' my_list = [] for letter in my_str: my_list.append(letter) # 👇️ ['b', 'o', 'b', 'b', 'y'] print(my_list)
We used a for loop to iterate over the string and use the append method to add each letter to the list.
The list.append() method adds an item to the end of the list.
The method returns None as it mutates the original list.
You can also conditionally add the letter to the list.
Copied!my_str = 'bobby' my_list = [] for letter in my_str: if letter.strip() != '': my_list.append(letter) # 👇️ ['b', 'o', 'b', 'b', 'y'] print(my_list)
The string is only added to the list if it isn’t a space.
# Split a String into a List of Characters using iterable unpacking
You can also use the iterable unpacking * operator to split a string into a list of characters.
Copied!my_str = 'bobby' my_list = [*my_str] print(my_list) # 👉️ ['b', 'o', 'b', 'b', 'y']
Notice that we wrapped the string in a list before using iterable unpacking.
The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.
Copied!example = (*(1, 2), 3) # 👇️ (1, 2, 3) print(example)
# Split a String into a List of Characters using extend
You can also use the list.extend() method to split a string into a list of characters.
Copied!my_str = 'bobby' my_list = [] my_list.extend(my_str) print(my_list) # 👉️ ['b', 'o', 'b', 'b', 'y']
The list.extend method takes an iterable and extends the list by appending all of the items from the iterable.
Copied!my_list = ['bobby'] my_list.extend(['hadz', '.', 'com']) print(my_list) # 👉️ ['bobby', 'hadz', '.', 'com']
The list.extend method returns None as it mutates the original list.
We can directly pass a string to the list.extend() method because strings are iterable.
Each character of the string gets added as a separate element to the list.
# Split a String into a List of Characters using map()
You can also use the map() function to split a string into a list of characters.
Copied!my_str = 'bobby' my_list = list(map(lambda char: char, my_str)) print(my_list) # 👉️ ['b', 'o', 'b', 'b', 'y']
Instead of passing the string directly to the list() class, we used the map() function to get a map object containing the characters of the string.
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
The lambda function we passed to map gets called with each character of the string and returns it.
The last step is to convert the map() object to a list.
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
Python Split String By Character – Split String Using split() method
Hi everyone, in this Python Split String By Character tutorial, we will learn about how to split a string in python. Splitting string means breaking a given string into list of strings. Splitting string is a very common operation, especially in text based environment like – World Wide Web or operating in a text file. Python provides some string method for splitting strings. Let’s took a look how they works.
Python Split String By Character Tutorial – Getting Started With Examples
When splitting string, it takes one string and break it into two or more lists of substrings. Some time we may need to break a large string into smaller strings. For splitting string python provides a function called split().
What Is split() function ?
- split() method breaks a given string by a specified separator and return a list of strings.
- Separator is a delimiter at which the string splits. If the separator is not specified then the separator will be any whitespace. This is optional.
- Maxsplit is the maximum number of splits.The default value of maxsplit is -1, meaning, no limit on the number of splits. This is optional.
Splitting String By Whitespace
If we don’t specify any separator split() method takes any whitespace as separator.
Let’s understand it with an example.
So the output of this code is –
In the output, you can see that the string is broken up into a list of strings.
Splitting String By Colon ‘ : ‘
Now we will use : as a separator. Let’s see what happend.
You can see that the colon(:) has been removed and string is splitted.
Splitting String By Hash Tag ‘ # ‘
Now we will use # as a separator, so for this the code is following.
Let’s see the result
All the # has been removed and string is splitted into list of substrings.
Splitting String By Comma ‘ , ‘
Now let’s see how to split string by comma. So write the following code.
Splitting String By Position
Now if you want to split a string at particular position. For example string is “PythonIsProgrammingLanguage” , and you want to split this string at 4th position. So the code for this will be as follows –
You can see the string is broken up from 4th position and splitted into list of substrings.
Splitting String By A Particular Characters
Now we will split string by a particular character. Let’s take an example “Python Is Programming Language”, and now you have to split this string at character P . So code for this is like below.
- Here we have used P as a separator, so the result is as follows.
- All the P character has been removed from this string but whites paces are still in there because separator is a character.
Splitting String By Special Characters
Now we will see how to split string by special characters like \n, \t and so on. Let’s implement it in examples.
- Here i am taking a special character \t i.e., tab.
- This code will split string at \t .
- So see the result.
Splitting String By Maxsplit
Now we will see splitting string by maxsplit. It is a number, which tells us to split the string into maximum of provided number of times. So let’s move towards its implementation.
Now we will take an another example in which we will specify both separator as well as maxsplit.
And the output of this example is –
- We can see that when value of maxsplit is 4 then string is splitted into a list of substrings where first four string is splitted at # and remaining are same.
- When value of maxsplit is 1 then string is splitted into a list of substrings where only first string is splitted at # and remaining are same.
- When maxsplit is 0 then no splitting occur because the value is zero.
Recommended Posts
So friends, now i am wrapping up Python Split String By Character tutorial. If you have any query then please let me know this. And please share this post with your friends and suggest them to visit simplified python for best python tutorials. Thanks Everyone.