Split string into groups of 3 characters [duplicate] - python

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']

Related

How to convert a string filled with numbers to seperate integers in a list? [duplicate]

This question already has answers here:
How to convert a string of space- and comma- separated numbers into a list of int? [duplicate]
(6 answers)
Python - Convert string with numbers into a list of integers [duplicate]
(2 answers)
Convert string of numbers to list of numbers [duplicate]
(6 answers)
Change a string of integers separated by spaces to a list of int
(8 answers)
How to split a string of space separated numbers into integers?
(9 answers)
Closed 1 year ago.
So basically I want to convert a string which has multiple numbers to seperate integers in a list.
lst = []
s = '12 14 17'
Basically what I am trying to do is get the lst to be lst = [12,14,17] but I am having difficulty doing this since the string is one whole string and not one string per number.
You can split the string , then convert each elements to int -
>>> s = '12 14 17'
>>> list(map(int,s.split()))
[12, 14, 17]
>>>

Get each element in a list string [duplicate]

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 3 years ago.
I have a string that consists of a list which made of different product code.
"['B00GHX7H0A', 'B00FRERO7G', 'B00R68QXCS', 'B000Z65AZE', 'B07GFHJRMX', 'B074KGBGL7', 'B00R68QXJG', 'B00025WYZC', 'B07H3W9BM5', 'B00KOBT82G', 'B072N2M1P6', 'B071G8FG2N', 'B00FASVFI8', 'B00GHXE4N8', 'B00EPG2QJI', 'B01MQ4MEFE', 'B01M8ML0SY', 'B074KHCPLH', 'B004XQWY4W', 'B00FASV6UU', 'B01M31HJBJ', 'B00KC8TU7O', 'B00B9TU5T2', 'B00K75EZ04', 'B000Q2Y0FI', 'B00FEGOCCM', 'B00EPFXFBW', 'B00H6SQY3Q', 'B00HZAOWUC', 'B07GFJF1DN', 'B001WBS68E', 'B074KJZCPH']"
How can I extract each product code out of this string?
There is nother option than to replace the unnecessary characters and then splitting it by the commas:
myList = "['123','231','312','123']"
myList = myList.replace("[","").replace("]","").replace("'", "").split(",")
print(myList)
# output: ["123","231","312","123"]
But with this you would get problems if the items inside the list could potentially contain commas.

Split the string into words [duplicate]

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.

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