| by Arround The Web | No comments

Python Combine Single String into a List of String

In Python, once strings are created, they can not be changeable. In simple words, they are immutable and can be in multiple forms, such as country name, person name, formless text, or anything that can be defined by using the language. Users can convert the string into a list of strings by utilizing the built-in functions.

In this guide, we will explain the ways for converting strings into a list of strings.

How to Combine Single String into a List of String in Python?

To convert the string into a list of strings in Python, the below-stated methods are used:

How to Combine Single String into a List of String Using the “eval()” Method in Python?

The “eval()” is a Python built-in method that is used for converting a string into a list. The “eval()” method analyzes the specified whole expression passed on to this process and runs the expression inside the program code. In simple words, it translates a string within a program code.

Here, we first declared and initialized the “myStr1” and “myStr2” string variables. Then, created a list named “comStr” and initialized it with the string variables:

myStr1 = "'Hello', 'Its'"
myStr2 = "'LinuxHint', 'World'"
comStr = [myStr1, myStr2]

Next, invoked the “eval()” function along with the “join()” function that takes the list as a parameter to join all the provided strings and generate a new single list. Lastly, applied the “print()” statement to print the particular list:

listStr = eval('+'.join(comStr))
print(list(listStr))

According to provided output, the strings has been converted into a list of string:

How to Combine Single String into a List of String Using “ast()” Method in Python?

Another way to convert strings into a list of strings is using the “ast()” method in Python. In our below-given block of code, first, we have imported the “ast” library:

import ast

Then, we used the previously declared string variables and generate another blank Python list:

comStr = []

Next, we apply the “for” loop to increase the length of the list and passed the previously created string type variable as a parameter to the loop. The “m” exists in the loop which iterates over the string and saves the values in the “m” variable. Then, extend the Python list by adding the string into the “m” and apply the “ast.literal_eval()” method. Lastly, use the “len()” method inside the “print()” method to retrieve the resultant converted string:

for m in (myStr1, myStr2):
    comStr.extend(ast.literal_eval(m))
print(list(comStr))

Output

You have learned different ways to convert the string into a list of strings in Python.

Conclusion

Python’s built-in methods are used to convert the string into a list of strings, such as the “eval()” method and “ast()” method. The “eval()” method analyzes the specified whole expression passed on to this process and translates a string within a program code. In contrast, Python provides an “ast()” as a module used to convert strings into lists. This guide illustrated different methods for converting the string into a list of strings in Python.

Share Button

Source: linuxhint.com

Leave a Reply