Change language

Python | Program for converting a string to a list

|

In this program we will try to convert the given string to a list containing spaces or any other special characters, depending on the user’s choice. For this we use the split () method. 
Syntax :

string.split("delimiter")

Examples:

 Input: "Geeks for Geeks" Output: [’Geeks’,’ for’, ’Geeks’] Input:" Geeks-for-Geeks "Output: [’ Geeks’, ’for’,’ Geeks’] 

The split method is used to split strings and store them in a list. The built-in method returns a list of words in a string using the "delimiter" as the delimiter string. If no delimiter is specified or is missing, a different splitting algorithm is applied: a series of consecutive spaces are treated as a single delimiter, and the result is no leading or trailing blank lines if the string has a leading or trailing space. 
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:

 [’Geeks’,’ for’, ’Geeks’] 

Example 2:

# 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:

 [’Geeks’,’ for’, ’Geeks’] 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method