Python | Program to convert String to a List With Examples

In this program, we will attempt to change a given string over to a rundown, where spaces or some other unique characters, as per the client’s decision, are experienced. To do this we utilize the split() technique.

Python is one of the most famous programming dialects today, and in this instructional exercise, we will learn different subtleties of Python, remembering what is a rundown for python, what is a string, ways of changing a rundown to a string, and then some. We should begin.

What Is a List in Python?

A rundown in python is an arranged succession that can hold an assortment of item types, for example, number, character or float. A rundown in python is identical to an exhibit in other programming dialects. It is addressed utilizing square sections, and a comma(,) is utilized to isolate two items present in the rundown.

A rundown and an exhibit in other programming dialects vary in the way that a cluster just stores a comparative information type, implying that an exhibit is homogeneous in nature, yet a rundown in python can store various information types all at once, and in this manner it can either be homogeneous or heterogeneous. The following are a few instances of homogeneous and heterogeneous records in python:

What Is a String in Python?

A string in python is an arranged succession of characters. The highlight be noted here is that a rundown is an arranged grouping of article types and a string is an arranged succession of characters. This is the primary contrast between the two.

A succession is an information type made out of various components of similar information type, like whole number, float, character, and so on This implies that a string is a subset of succession information type, containing all components as characters.

Here is an instance of string in python and how to print it.

For announcing a string, we relegate a variable to the string. Here, a will be a variable doled out to the string simplilearn. A component of a string can be gotten to similarly as we found on account of a rundown. The ordering of components in a string likewise begins from 0.

Syntax:

string.split("delimiter")

Example 1:

# Python code to convert string to list
 
def Convert(string):
    li = list(string.split(" "))
    return li
 
# Driver code    
str1 = "Geeks for Geeks"
print(Convert(str1))

Output:

[eevibes]

Example 2:

The split technique is utilized to divide the strings and store them in the rundown. The implicit strategy returns a rundown of the words in the string, utilizing the “delimiter” as the delimiter string. If a delimiter isn’t determined or is None, an alternate parting calculation is applied: runs of sequential whitespace are viewed as a solitary separator, and the outcome will contain no vacant strings toward the beginning or end assuming that the string has driving or following whitespace.

# Python code to convert string to list
def Convert(string):
    li = list(string.split("-"))
    return li
 
# Driver code    
str1 = "Geeks-for-Geeks"
print(Convert(str1))

Output:

[eevibes]

Example 3:

# Python code to convert string to list character-wise
def Convert(string):
    list1=[]
    list1[:0]=string
    return list1
# Driver code
str1="ABCD"
print(Convert(str1))

Output:

['A','B','C','D']

Also ReadHow to send an email using PHPMailer ?

Leave a Reply

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