This question already has answers here:
Python string.strip stripping too many characters [duplicate]
(3 answers)
Strip removing more characters than expected
(2 answers)
How to remove the left part of a string?
(21 answers)
Closed 13 days ago.
I have the following list of elements named 'files_temp':
['CDS_SPREAD_AA1EUNBCBM', 'CDS_SPREAD_AA1EUNCCBM', 'CDS_SPREAD_AA1USNBCBM', 'CDS_SPREAD_AA1USNCCBM', 'CDS_SPREAD_AALLN1EUNECBM', 'CDS_SPREAD_AALLN1USNECBM', 'CDS_SPREAD_ABB3EUNECBM', 'CDS_SPREAD_ABB3USNECBM', 'CDS_SPREAD_ABX1EUNCCBM', 'CDS_SPREAD_ABX1USNCCBM', 'CDS_SPREAD_ACAFP1EUBECBM', 'CDS_SPREAD_ACAFP1EUNECBM', 'CDS_SPREAD_ACOM1JPNACBM', 'CDS_SPREAD_ACOM1USNACBM', 'CDS_SPREAD_AEGON1EUBACBM', 'CDS_SPREAD_AEGON1EUNECBM', 'CDS_SPREAD_AEGON1JPBACBM', 'CDS_SPREAD_AEGON1USBACBM', 'CDS_SPREAD_AEGON1USNECBM', 'CDS_SPREAD_AEP1USNBCBM' ...]
I would like to keep only the alphanumeric codes, removing the CDS_SPREAD_ part and tried the following code:
files_temp=[elem.strip('CDS_SPREAD_') for elem in files_temp]
However, besides the CDS_SPREAD_ part it is also removing a part of the alphanumeric code:
['1EUNBCBM', '1EUNCCBM', '1USNBCBM', '1USNCCBM', 'LLN1EUNECBM', 'LLN1USNECBM', 'BB3EUNECBM', 'BB3USNECBM', 'BX1EUNCCBM', 'BX1USNCCBM', 'FP1EUBECBM', 'FP1EUNECBM', 'OM1JPNACBM', 'OM1USNACBM', 'GON1EUBACBM', 'GON1EUNECBM', 'GON1JPBACBM', 'GON1USBACBM', 'GON1USNECBM', '1USNBCBM', '1USNCCBM', 'T1EUNCCBM', 'T1USNBCBM' ...]
For instance, for the first element, in theory I should get AA1EUNBCBM instead of 1EUNBCBM. Would you know why this is happening? I would highly appreciate an alternative to solve the issue as well.
The strip() function removes all the characters you are providing as the parameters. For your case, you should use replace() function.
files_temp=[elem.replace('CDS_SPREAD_', '') for elem in files_temp]
This question already has answers here:
Efficient way to add spaces between characters in a string
(5 answers)
Closed 2 years ago.
I need to insert a '*' between each two characters in a string in Python, for example, for 'ABCDE' I need to get 'A*B*C*D*E'. What's a Python way of doing it?
This could be a solution
string ='ABCDE'
x = '*'
print(x.join(string))
This question already has answers here:
Slice every string in list in Python
(3 answers)
What is the purpose of the two colons in this Python string-slicing statement?
(1 answer)
Closed 4 years ago.
If i had a list as an example:
a = ['Hello_1.txt', 'Hello_2.txt']
In Python is it possible to somehow remove the first 5 characters ('Hello')
and the last 3 characters ('txt') from each of the items in the list ?
You could use a list-comprehension and string slicing:
[s[5:-3] for s in a]
which gives what you describe (not sure this is the neatest output though!)
['_1.', '_2.']
This question already has answers here:
why is python string split() not splitting
(3 answers)
Closed 6 years ago.
I'm trying to split
<team>
into just team, here is the code I'm using:
s = "<team>"
s.split(">")[1]
s
'<team>'
s.split(">")[1].split("<")[0]
s
'<team>
As you can see, it's still leaving me with
<team>
anyone know why>
str.split() function returns a list, it does not split the string in place.
You'll need to make a new variable:
s = "<team>"
t = s.split(">")[1]
t
This question already has answers here:
How do I reverse a string in Python?
(19 answers)
How do I reverse each word in a string, but in the same sentence order? [duplicate]
(1 answer)
Closed 6 years ago.
How to reverse string in python? my input string is "Hey Gun" now i want to display "Gun Hey" as output. I have tried using slice operator like [::-1] but it won't shows proper output how it works in python?
Do splitting and then reversing and then joining.
' '.join(string.split()[::-1])
Using split and reversed (a bit slower though):
>>> a
'Hey Gun'
>>> ' '.join(reversed(a.split()))
'Gun Hey'