![]() |
|
[Tut] Python | Split String into Characters - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python | Split String into Characters (/thread-100125.html) |
[Tut] Python | Split String into Characters - xSicKxBot - 10-22-2022 Python | Split String into Characters Rate this post Summary: Use the Code: list("given string")Minimal Example: Code: print(list("abc"))Problem: Given a string; How will you split the string into a list of characters? Example: Let’s visualize the problem with the help of an example: input = “finxter” output = [‘f’, ‘i’, ‘n’, ‘x’, ‘t’, ‘e’, ‘r’] Now that we have an overview of our problem let us dive into the solutions without further ado. [b]Method 1: Using The list Constructor[/b] Approach: One of the simplest ways to solve the given problem is to use the list constructor and pass the given string into it as the input. Code: list()Code: Code: text = "finxter"
print(list(text)) # ['f', 'i', 'n', 'x', 't', 'e', 'r'] Related Tutorial: Python list() — A Simple Guide with VideoMethod 2: Using a List Comprehension Another way to split the given string into characters would be to use a list comprehension such that the list comprehension returns a new list containing each character of the given string as individual items. Code: Code: text = "finxter"
print([x for x in text]) # ['f', 'i', 'n', 'x', 't', 'e', 'r']Prerequisite: To understand what happened in the above code, it is essential to know what a list comprehension does. In simple words, a list comprehension in Python is a compact way of creating lists. The simple formula is Code: [expression + context]Code: forCode: ifExplanation: Well! Now that you know what list comprehensions are, let’s try to understand what the above code does. In our solution, the context variable Code: xCode: forCode: xMulti-line Solution: Another approach to formulating the above solution is to use a for loop. The idea is pretty similar; however, we will not be using a list comprehension in this case. Instead, we will use a for loop to iterate across individual characters of the given string and store them one by one in a new list with the help of the append method. Code: text = "finxter"
res = []
for i in text: res.append(i)
print(res) # ['f', 'i', 'n', 'x', 't', 'e', 'r']Yet another way of solving the given problem is to use a lambda function within the Code: mapCode: import re
text = "finxter"
print(list(map(lambda c: c, text))) # ['f', 'i', 'n', 'x', 't', 'e', 'r']Explanation: The Code: map()Code: list()[b]Conclusion[/b] Hurrah! We have successfully solved the given problem using as many as three different ways. I hope you enjoyed this article and it helps you in your Python coding journey. Please subscribe and stay tuned for more interesting articles! Related Reads: ⦿ How To Split A String And Keep The Separators? ⦿ How To Cut A String In Python? |