Python - Breaking Up String Variables
Last Updated :
25 Feb, 2023
Prerequisite: itertools
Given a String, the task is to write a Python program to break up the string and create individual elements.
Input : GeeksForGeeks
Output : [ 'G', 'e', 'e', 'k', 's', 'F', 'o', 'r', 'G', 'e', 'e', 'k', 's' ]
Input: Computer
Output: [ 'C', 'o', 'm', 'p', 'u', 't', 'e', 'r']
Below are some ways to do the above task.
Method #1: Using join() function
The join() method provides a flexible way to create strings from iterable objects. It joins each element of an iterable (such as list, string, and tuple) by a string separator (the string on which the join() method is called) and returns the concatenated string.
Python3
a = "GeeksForGeeks"
split_string = list(''.join(a))
print(split_string)
Output:
['G', 'e', 'e', 'k', 's', 'F', 'o', 'r', 'G', 'e', 'e', 'k', 's']
Time Complexity: O(n2) -> (join+conversion to list)
Space Complexity: O(n)
Method #2: Using for loop
Python3
a = "GeeksForGeeks"
res = [i for ele in a for i in ele]
print(res)
Output:
['G', 'e', 'e', 'k', 's', 'F', 'o', 'r', 'G', 'e', 'e', 'k', 's']
Time Complexity: O(n2)
Space Complexity: O(n)
Method #3: Using chain.from_iterable()
Python3
from itertools import chain
a = "GeeksForGeeks"
# using chain.from_iterable()
# to convert list of string and characters
# to list of characters
res = list(chain.from_iterable(a))
# printing result
print(str(res))
Output:
['G', 'e', 'e', 'k', 's', 'F', 'o', 'r', 'G', 'e', 'e', 'k', 's']
Time Complexity: O(n2) -> (chain+converting to list)
Space Complexity: O(n)
Method #5: Using map() function
Here is the step-by-step algorithm for the given code:
Define a string variable "a" and initialize it with the value "GeeksForGeeks".
Define a lambda function that takes a character as an input and returns that character.
Use the map() function to apply the lambda function to each character in the string.
Store the result in a list named "result".
Print the list "result".
Python3
a = "GeeksForGeeks"
result = list(map(lambda x: x, a))
print(result)
Output['G', 'e', 'e', 'k', 's', 'F', 'o', 'r', 'G', 'e', 'e', 'k', 's']
Time complexity:
The time complexity of the map() function is O(n), where n is the length of the input string. In this case, the map() function is applied to each character in the string, so the time complexity of the code is O(n).
Space complexity:
The space complexity of the code is O(n), where n is the length of the input string. The map() function creates a new list of characters that is the same length as the input string, so the space complexity of the code is O(n).
Similar Reads
Insert a Variable into a String - Python
The goal here is to insert a variable into a string in Python. For example, if we have a variable containing the word "Hello" and another containing "World", we want to combine them into a single string like "Hello World". Let's explore the different methods to insert variables into strings effectiv
2 min read
Python - Line Break in String
Line Break helps in making the string easier to read or display in a specific format. When working with lists, the join() method is useful, and formatted strings give us more control over our output.Using \n for new line The simplest way to add a line break in a string is by using the special charac
2 min read
Python - Uppercase Half String
The problem is to convert half of a string to uppercase, either the first half or the second half, depending on the requirement. For example, given the string "python", the output could be "PYThon" (uppercase first half) or "pytHON" (uppercase second half).If the string length is odd, handle the mid
2 min read
Convert tuple to string in Python
The goal is to convert the elements of a tuple into a single string, with each element joined by a specific separator, such as a space or no separator at all. For example, in the tuple ('Learn', 'Python', 'Programming'), we aim to convert it into the string "Learn Python Programming". Let's explore
2 min read
Convert String to Tuple - Python
When we want to break down a string into its individual characters and store each character as an element in a tuple, we can use the tuple() function directly on the string. Strings in Python are iterable, which means that when we pass a string to the tuple() function, it iterates over each characte
2 min read
Python - Words Lengths in String
We are given a string we need to find length of each word in a given string. For example, we are s = "Hello world this is Python" we need to find length of each word so that output should be a list containing length of each words in sentence, so output in this case will be [5, 5, 4, 2, 6].Using List
2 min read
Python - Triple quote String concatenation
Sometimes, while working with Python Strings, we can have a problem in which we need to perform concatenation of Strings which are constructed by Triple quotes. This happens in cases we have multiline strings. This can have applications in many domains. Let us discuss certain ways in which this task
4 min read
Write Multiple Variables to a File using Python
Storing multiple variables in a file is a common task in programming, especially when dealing with data persistence or configuration settings. In this article, we will explore three different approaches to efficiently writing multiple variables in a file using Python. Below are the possible approach
2 min read
String Interning in Python
String interning is a memory optimization technique used in Python to enhance the efficiency of string handling. In Python, strings are immutable, meaning their values cannot be changed after creation. String interning, or interning strings, involves reusing existing string objects rather than creat
2 min read
Python - Word starting at Index
Sometimes, while working with Python, we can have a problem in which we need to extract the word which is starting from a particular index. This can have a lot of applications in school programming. Let's discuss certain ways in which this task can be performed. Method #1: Using a loop This is a bru
2 min read