[PYTHON] Make a list in which characters are stored as one element into a string

What you want to do

① Put each character string in the list as an element (2) Convert the characters in the list as elements one by one into a character string.

python


#① Put each character string in the list as an element
'Hello'
   ↓
['H', 'e', 'l', 'l', 'o']

#(2) Convert the characters in the list as elements one by one into a character string.
['H', 'e', 'l', 'l', 'o']
   ↓
'Hello'

motion

Premise

program


strs = 'Hello'
print(strs)
print(type(strs))

output


Hello
<class 'str'>

① Put each character string in the list as an element

What to do: Just put it in ** list () **

program


strs = 'Hello'
strs_list = list(strs) #← Only here

print(strs_list)
print(type(strs_list))

output


['H', 'e', 'l', 'l', 'o']
<class 'list'>

(2) Convert the characters in the list as elements one by one into a character string.

What to do: Create an empty list and add elements one by one.

program


strs = 'Hello'
strs_list = list(strs)

new_strs = '' #Create an empty list to make a string
for i in range(len(strs_list)):
    new_strs += strs_list[i]

print(new_strs)
print(type(new_strs))

output


Hello
<class 'str'>

Recommended Posts

Make a list in which characters are stored as one element into a string
How to make a string into an array or an array into a string in Python
Make a copy of the list in Python
Make a rock-paper-scissors game in one line (python)
How to identify the element with the smallest number of characters in a Python list?
Make one repeating string with a Python regular expression.
Make a table of multiplication of each element in a spreadsheet (Python)
Divide a set of integers into cosets in one shot
Extract the value of dict or list as a string
How to connect the contents of a list into a string