This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Closed 5 years ago.
Suppose I get input as apple, how can I split it in list of each character like ['a','p','p','l','e']?
I tried [i for i in input().split('')],
but it outputs error: ValueError: empty separator.
Try list('apple').
This will split the string 'apple' into individual characters and place them inside a list.
Output:
['a','p','p','l','e']
Related
This question already has answers here:
Given a string how can I remove all the duplicated consecutive letters?
(5 answers)
Closed 1 year ago.
How can i make a basic function with def in python that removes duplicate letters from a string?
Example: input: "abbcdddea"; output: "abcdea"
This code removes duplicates but conserves the order:
string = "abb"
string_without_duplicates = "".join(dict.fromkeys(string))
This question already has answers here:
How to split strings inside a list by whitespace characters
(4 answers)
Split a list of strings by comma
(4 answers)
How do I split a string into a list of words?
(9 answers)
Closed 2 years ago.
I have a list that looks like:
test = ['bla bla','affect /affected / affecting']
print(test)
and I would like to create new elements in the list by separating the ones that are one element but separated through the "/" symbol.
E.g. desired output in this case:
test_new = ['bla bla','affect','affected','affecting']
How can I do that?
EDIT: The list can have multiple elements, and not all have the "/" symbol
You can use a list comprehension and split on '/' and strip to remove spaces (assuming you have multiple strings, being a list):
[j.strip() for i in test for j in i.split('/')]
# ['affect', 'affected', 'affecting']
testEntry=test[0]
testEntry=test.replace(' ','')
test=testEntry.split('/')
This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
How can I make a word from all the letters in a string?
For example:
(this is my list)
["h","e","l","l","o"]
And I want this as output:
hello
Try this :
"".join(["h","e","l","l","o"])
Use the join function which concatenate all the characters/substrings present in the list & return a single string.
name = ["h","e","l","l","o"]
concat_name = "".join(name)
print(concat_name)
Output :
hello
Try this :
''.join(["h","e","l","l","o"])
This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Closed 5 years ago.
How would you go about separating each character in a given input and turn it into a list?
For example I have
import string
print ("Enter string")
x = input("")
Enter string
The quick brown
I want the end result to be
['T','h','e',' ','q','u','i','c','k',' ','b','r','o','w','n']
Y'know, to turn every character as a separate string in a list instead of every word as a separate string.
Thanks!
Simply use list(x) where x is the string.
This question already has answers here:
Convert a list of characters into a string [duplicate]
(9 answers)
Closed 6 years ago.
I have this list :
listOfWords = ["this","is","a","list","of","words"]
and I need to have [this is a list of words], with a List comprehension.
You mean a list with a single string item?
>>> [' '.join(["this","is","a","list","of","words"])]
['this is a list of words']