Split the string into words [duplicate] - python

This question already has answers here:
Split string every nth character?
(19 answers)
Closed 4 years ago.
how to split a string into words of 2 letters. Like given string is "HelloThere" now i want to make it ["He","ll","oT","he","re"]. Please help to code that in python.

yourList = []
yourString = "HelloThere"
while yourString:
yourList.append(yourString[:2])
yourString = yourString[2:]
If you print yourList, you will get the result.

Related

How to get the numbers from a string (contains no spaces between letters and numbers)? [duplicate]

This question already has answers here:
How to extract numbers from a string in Python?
(19 answers)
Closed 3 years ago.
So, I have a string "AB256+74POL". I want to extract the numbers only into a list say num = [256,74]. How to do this in python?
I have tried string.split('+') and followed by iterating over the two parts and adding the characters which satisfy isdigit(). But is there an easier way to that?
import re
a = 'AB256+74POL'
array = re.findall(r'[0-9]+', a)
"".join([c if c.isdigit() else " " for c in mystring]).split()
Explanation
Strings are iterable in python. So we iterate on each character in the string, and replace non digits with spaces, then split the result to get all sequences of digits in a list.

check strings of a list [duplicate]

This question already has answers here:
Check if multiple strings exist in another string
(17 answers)
Closed 4 years ago.
I want to when I input a string, if words in string is contain in a list of words, delete words in the strings
Or, you could do the following:
raw_string = input("Enter String:")
useless_list = ["Birds", "Cat"]
print(' '.join([i for i in raw_string.split() if i not in useless_list]))

Split string into groups of 3 characters [duplicate]

This question already has answers here:
Split string every nth character?
(19 answers)
Closed 5 years ago.
I have a string
ddffjh3gs
I want to convert it into a list
["ddf", "fjh", "3gs"]
In groups of 3 characters as seen above.
What is the best way to do this in python 2.7?
Using list comprehension with string slice:
>>> s = 'ddffjh3gs'
>>> [s[i:i+3] for i in range(0, len(s), 3)]
['ddf', 'fjh', '3gs']

Eliminate numbers in string in Python [duplicate]

This question already has answers here:
Remove specific characters from a string in Python
(26 answers)
Removing numbers from string [closed]
(8 answers)
Closed 8 years ago.
I’d like to eliminate numbers in a string in Python.
str = "aaaa22222111111kkkkk"
I want this to be "aaaakkkkk".
I use re.sub to replace, but it doesn't work:
str = "aaaa22222111111kkkkk"
str = re.sub(r'^[0-9]+$',"",str)
Maybe, this replaces a string which only contains numbers with "".
How should I do with this?
your regex is wrong:
re.sub(r'[0-9]',"",str)
should work:
>>> str="aaaa22222111111kkkkk"
>>> re.sub(r'[0-9]',"",str)
'aaaakkkkk'

Find last occurrence of character in string Python [duplicate]

This question already has answers here:
Find index of last occurrence of a substring in a string
(12 answers)
Closed 8 years ago.
How would I find the last occurrence of a character in a string?
string = "abcd}def}"
string = string.find('}',last) # Want something like this
You can use rfind:
>>> "abcd}def}".rfind('}')
8

Categories

Resources