This question already has answers here:
Extracting only characters from a string in Python
(7 answers)
How do you filter a string to only contain letters?
(6 answers)
Closed 6 years ago.
I was trying to figure out how to list just the letters in a string and ignore the numbers or any other characters. I figured out how to do it using the for loop, but I couldn't find out how to do it without using the for loop.
This is how I used the for loop:
>>> a = "Today is April 1, 2016"
for i in a:
if i.isalpha():
list(i)
Any help will be appreciated!
You can use filter for this:
>>> ''.join(filter(str.isalpha, a))
'TodayisApril'
list(set([x for x in a if x.isalpha()]))
this should do it :)
Related
This question already has answers here:
Understanding slicing
(38 answers)
Changing one character in a string
(15 answers)
Closed 1 year ago.
I know how to replace characters, but I would like to have the instance done once. My code replace's all the characters before.
string = "Forever9999"
string = string[:-4] + string[-4:].replace("9", "1")
Which in the end would be Forever1999, but I get Forever1111
Any help appreciated.
You can pass another paramter to str.replace(old, new[, count]), which is the max count of occurrences you want to replace:
string.replace("9", "1", 1)
# 'Forever1999'
This question already has answers here:
How to remove substring from string in Python 3
(2 answers)
Closed 2 years ago.
Is there I way to delete words from a string in Python if it doesn't have spaces. For example, if you have the string "WUBHELLOWUB" I want to remove "WUB". I tried
s = 'WUBHELLOWUB'
while 'WUB' in s:
ind = s.find('WUB')
s = s[:ind] + s[ind+1:]
print(s)
but it did not work.
You can use regex
import re
data=r"\S*WUB\S*"
re.sub(data, '','WUBWUBHELLO')
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 to replace multiple substrings of a string?
(28 answers)
Closed 5 years ago.
I am working with Python 3 and I want to replace the emoticons included in a dictionary.
For example
text = "Hi, I'm coming home :)"
#Create dictionary
dict_lookup = {':(' : 'sad',
':)' : 'happy'}
The desired output is:
Hi, I'm coming home happy
What is the most efficient way to achieve this result in Python 3?
This should do the trick:
for emote, replacement in dict_lookup.items():
text = text.replace(emote, replacement)
Take a look at str.replace
It allows you to do text.replace(dict_key, dict_value)
This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Closed 7 years ago.
I have the following string: "aaaabbbb"
How can I get the last four characters and store them in a string using Python?
Like this:
>>> mystr = "abcdefghijkl"
>>> mystr[-4:]
'ijkl'
This slices the string's last 4 characters. The -4 starts the range from the string's end. A modified expression with [:-4] removes the same 4 characters from the end of the string:
>>> mystr[:-4]
'abcdefgh'
For more information on slicing see this Stack Overflow answer.
str = "aaaaabbbb"
newstr = str[-4:]
See : http://codepad.org/S3zjnKoD