Python Convert String to List

Convert string to list of characters and list to string python;Through this tutorial, i am going to show you how to convert string to list of characters and list to string in python.

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

There are two ways to convert string to list of character and list of character 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

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.

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

See the following python program 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']

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

See the following 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

See 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 Tutorials

Recommended:-Python Lists
Recommended:-Python Strings

Be First to Comment

Leave a Reply

Your email address will not be published.