How do I turn a list of numbers into a string?

Python Program to convert List of Integer to List of String

Given a List of Integers. The task is to convert them to List of Strings.

Examples:

Input: [1, 12, 15, 21, 131] Output: ['1', '12', '15', '21', '131'] Input: [0, 1, 11, 15, 58] Output: ['0', '1', '11', '15', '58']

Method 1: Using map[]




# Python code to convert list of

# string into sorted list of integer

# List initialization

list_int = [1, 12, 15, 21, 131]

# mapping

list_string = map[str, list_int]

# Printing sorted list of integers

print[list[list_string]]

Output:

['1', '12', '15', '21', '131']

Example 2: Using list comprehension






# Python code to convert list of

# string into sorted list of integer

# List initialization

list_string = [1, 12, 15, 21, 131]

# Using list comprehension

output = [str[x] for x in list_string]

# Printing output

print[output]

Output:

['1', '12', '15', '21', '131']

Method 3: Using iteration




# Python code to convert list of

# string into sorted list of integer

# List initialization

list_string = [1, 12, 15, 21, 131]

list_int = []

# using iteration and sorted[]

for i in list_string:

list_int.append[i]

# printing output

print[list_int]

Output:

['1', '12', '15', '21', '131']




Article Tags :

Python

Python list-programs

python-list

Practice Tags :

python-list

Read Full Article

Method 1: List Comprehension

Suppose we have a list:

a = [4, 3, 2, 1, 10, 14, -14]

Now, check the type of the list numbers:

print[type[a[0]]] #

Let’s apply the built-in function str[], and get a list of strings using list comprehension:

print[[str[x] for x in a]] # ['4', '3', '2', '1', '10', '14', '-14']

?List comprehension is a compact way of creating lists. The simple formula is [expression + context]. Expression: What to do with each list element? Context: What elements to select? The context consists of an arbitrary number of for and if statements.

You can watch me explain list comprehensions in this video:

Check the type of numbers in the new list:

A = [str[x] for x in a] print[type[A[0]]] #

The built-in function str converts an integer to a string representation of that integer. Thus, it helps us create a new list of strings from the list of integers in a single line of code.

Convert a list of strings and a list of numbers to each other in Python

Posted: 2021-01-04 / Tags: Python, String, List

Tweet

This article describes how to convert a list of strings [str] and a list of numbers [int, float] to each other in Python with sample code.

  • Convert a list of numbers [int, float] to a list of strings [str]
    • Convert numbers to decimal strings
    • Convert numbers to binary, octal, and hexadecimal strings
    • Convert numbers to exponential strings
  • Convert a list of strings [str] to a list of numbers [int, float]
    • Convert decimal strings to numbers
    • Convert binary, octal, and hexadecimal strings to numbers
    • Convert exponential strings to numbers
    • Convert only strings that can be converted to numbers

List comprehension is useful to generate a new list from another list. The sample code in this article also uses the list comprehensions. See the following article for details.

  • List comprehensions in Python

See the following article for more details on converting strings to numbers.

  • Convert a string to a number [int, float] in Python

Sponsored Link

4 Ways to Convert List to String in Python

While working with python data types, there are situations when you want to convert the data collected from one type to another. There are 4 methods by which we can convert a list to a string in python with fewer lines of code. These methods of converting a list into a string include iteration, comprehension, join[], and map[] method. But before understanding all these conversion methods in detail, let us understand what are lists and strings.

Various methods to convert lists to string in python?

In this short tutorial, we look at all the different methods and the code that can be used to convert list to string in Python.

Video liên quan

Bài mới nhất

Chủ Đề