Introduction
While programming we may need to convert a string to list in Python. That could be for any other reason. But, a question arises here, how can we convert a string to different forms of lists?
So, here in this tutorial, we are going to learn how we can convert a string into a list in Python.
Convert string to list in Python
In this short tutorial, find how to convert string to list in Python. We look at all the ways you can achieve this along with their pros and cons.Python | Program to convert String to a List
In this program, we will try to convert a given string to a list, where spaces or any other special characters, according to the users choice, are encountered. To do this we use the split[] method.
Syntax:
Examples:
Input : "Geeks for Geeks" Output : ['Geeks', 'for', 'Geeks'] Input : "Geeks-for-Geeks" Output : ['Geeks', 'for', 'Geeks']The split method is used to split the strings and store them in the list. The built-in method returns a list of the words in the string, using the “delimiter” as the delimiter string. If a delimiter is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.
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']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']Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Convert string to integer in Python
In Python an strings can be converted into a integer using the built-in int[] function. The int[] function takes in any python data type and converts it into a integer.But use of the int[] function is not the only way to do so. This type of conversion can also be done using thefloat[] keyword, as a float value can be used to compute with integers.
Below is the list of possible ways to convert an integer to string in python:
1. Using int[] function
Syntax: int[string]
Example:
num = '10'
# check and print type num variable
print[type[num]]
# convert the num into string
converted_num = int[num]
# print type of converted_num
print[type[converted_num]]
# We can check by doing some mathematical operations
print[converted_num + 20]
|
As a side note, to convert to float, we can use float[] in Python
num = '10.5'
# check and print type num variable
print[type[num]]
# convert the num into string
converted_num = float[num]
# print type of converted_num
print[type[converted_num]]
# We can check by doing some mathematical operations
print[converted_num + 20.5]
|
2. Using float[] function
We first convert to float, then convert float to integer. Obviously the above method is better [directly convert to integer]
Syntax: float[string]
Example:
a = '2'
b = '3'
# print the data type of a and b
print[type[a]]
print[type[b]]
# convert a using float
a = float[a]
# convert b using int
b = int[b]
# sum both integers
sum = a + b
# as strings and integers can't be added
# try testing the sum
print[sum]
|
Output:
class 'str' class 'str' 5.0Note: float values are decimal values that can be used with integers for computation.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course