Friday, December 27, 2024
Google search engine
HomeLanguagesPython Convert String to List and List to String

Python Convert String to List and List to String

Convert string to list of characters and list to string python; In this tutorial, you will learn two methods of how to Convert string to list of characters and list to string in python.

.medrectangle-3-multi-320{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:auto!important;margin-right:auto!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:250px;padding:0;text-align:center!important;width:100%}

Python programming language, the data type conversion is the most basic things. In this tutorial, you will see how to convert string to list in python and as well as how to convert python list to string with examples.

How to Convert String to List and List to String in Python

  • 1 Method: Python Convert String to List
  • 2 Method: Python Convert List to String

1 Method: Python Convert String to List

Python programming uses the split() function of Python to convert a string to a list.

Let’s know about the split() function of Python.

Definition of python split() function :- The pyhon split() function/method splits a given string into the python list.

Syntax of split() function is :-

string.split(separator, maxsplit)

Return Value from split() :-

The split() breaks the string at the separator and returns a list of strings.

.box-4-multi-260{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:auto!important;margin-right:auto!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:250px;padding:0;text-align:center!important;width:100%}

Example 1: Convert String to List in Python Using Split() Function

Let’s look at a simple example where we want to convert a string to a list of words i.e. split it with the separator as white spaces.

text= 'Welcome to python world'

# splits at space
print(text.split())

text2 = 'Welcome, to next, python, world'

# splits at ','
print(text2.split(', '))

# Splitting at ':'
print(text2.split(':'))

Output

 ['Welcome', 'to', 'python', 'world']
 ['Welcome', 'to next', 'python', 'world']
 ['Welcome, to next, python, world']

Note that, convert the list to string in python, use the for loop and join() function.

Example 2: Python split() works when maxsplit is specified

text = 'Welcome, to python, world'

# maxsplit: 2
print(text.split(', ', 2))

# maxsplit: 1
print(text.split(', ', 1))

# maxsplit: 5
print(text.split(', ', 5))

# maxsplit: 0
print(text.split(', ', 0))

Output

 ['Welcome', 'to python', 'world']
 ['Welcome', 'to python, world']
 ['Welcome', 'to python', 'world']
 ['Welcome, to python, world']

2 Method: Python Convert List to String

Example 1: python list to string using for loop

Using the python for loop convert a list to string in python. You can see below the python program to convert the list to string using for loop:

# Python program to convert a list to string 
	
# Function to convert 
def listToString(s): 
	
	# initialize an empty string 
	str1 = "" 
	
	# traverse in the string 
	for ele in s: 
		str1 += ele 
	
	# return string 
	return str1 
		
		
# Driver code	 
s = ['Hello', 'python', 'programmer'] 
print(listToString(s)) 

Output

Hellopythonprogrammer

Example 2: python convert list to string using join

Using the python join() function convert a list to string in python. You can see below the python program to convert the list to string using python join() function:

# Python program to convert a list 
# to string using python join() method 
    
# Function to convert   
def listToString(s):  
    
    # initialize an empty string 
    str1 = " " 
    
    # return string   
    return (str1.join(s)) 
        
        
# Driver code     
s = ['Hello', 'python', 'dev'] 
print(listToString(s)) 

Output

 Hello python dev

Question 1:- In Python, how do I convert a string to a list in which each character will be separated?

Answer :- You can convert string word to list, so you can use list() method in python. See the following example below:

string = 'python'

convertToList = list(string)

print(convertToList)

Output

['p', 'y', 't', 'h', 'o', 'n']

Recommended Python Programs

RELATED ARTICLES

Most Popular

Recent Comments