Python 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.

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.

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *