Which of the following function convert a string to list in Python?

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:

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




Article Tags :
Python
Python list-programs
Python string-programs
python-list
python-string
Practice Tags :
python-list

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

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




Article Tags :
Python
Write From Home
python-basics
Python-datatype
python-string

How to Convert String to List in Python

The data type conversion is the most common operation in the Python language. In this tutorial, we will see How To Convert Python String to List and List to String. Then, we will try to convert the given string to a list, where spaces or any other special characters.

Python string to list

To convert string to list in Python, use the string split() method. Thesplit() method splits the strings and store them in the list. Then, itreturns a list of the words in the string, using the “delimiter”as the delimiter string.

If the delimiter is not specified in the function argument or is None, then a different splitting algorithm is applied: its runs of consecutive whitespace are regarded as the single separator.

The result will contain no empty strings at the start or end of the string if the string has leading or trailing whitespace.

See the following code example to understand more.

# app.py def stringToList(string): listRes = list(string.split(" ")) return listRes strA = "Millie Bobby Brown" print(stringToList(strA))

See the output.

➜ pyt python3 app.py ['Millie', 'Bobby', 'Brown'] ➜ pyt

You can check the data type using the Python type() function.

# app.py def stringToList(string): listRes = list(string.split(" ")) return listRes strA = "Millie Bobby Brown" print(type(stringToList(strA)))

Output

➜ pyt python3 app.py ➜ pyt