user278618
user278618

Reputation: 20242

Is it a simplest substring in python?

Below is my code which get code(always in brackets, and always has 3 chars) from string :

raw_text='Spain (BCN)' #another examples: 'Italy (BGN)' , 'Germany (SXF)'
formatted_text=raw_text[raw_text.index('(')+1:len(raw_text)-1] # BCN,BGN,SFX

Can I write it simpler?

Upvotes: 1

Views: 187

Answers (7)

Tim Pietzcker
Tim Pietzcker

Reputation: 336198

import re
raw_text='Spain (BCN)'
formatted_text = re.search(r"""
    (?<=\() # assert that the preceding character is a (
    \w{3}   # match three alphanumeric characters
    (?=\))  # assert that the following character is a )""", 
    raw_text, re.VERBOSE).group(0)

would be another way of doing it (with a regular expression).

Upvotes: 3

John Machin
John Machin

Reputation: 82934

>>> raw_text='Spain (BCN)'
>>> formatted_text=raw_text[raw_text.index('(')+1:len(raw_text)-1]
>>> formatted_text
'BCN'
>>> raw_text[raw_text.index('(')+1:-1]
'BCN'
>>>

What an allegedly non-brittle does:

>>> str = 'abcdefgh'
>>> str[str.find('(')+1:str.find(')')]
'abcdefg'
>>>

Upvotes: 0

Will
Will

Reputation: 75635

splicing a string is [start:stop] and you are stopping on len(raw_text)-1 - always the second-last character. If you know the code continues to the end of the string, and as you've said it is always three characters long, then:

 formatted_text=raw_text[-4:-1]

will extract the three characters that start 4 from the end of the string

Upvotes: 1

John Ballinger
John Ballinger

Reputation: 7550

Yip there sure is.

raw_text='Spain (BCN)  '
print raw_text.rstrip(" ")[-4:-1] 

Use rstrip to remove trailing spaces, eg trim. Then simply go back 4 chars, to -1 chars.

Upvotes: 1

Dimi
Dimi

Reputation: 64

If you are certain to have this format why not just use:

s.strip()[-4: -1]

Of course, it does not check the format of your string. If you want to do that, use the re module (regular expressions).

Hope this helps,

Dimi

Upvotes: 3

Andrea
Andrea

Reputation: 20493

You can use a regex

>>> import re
>>> re.search('\((.{3})\)', 'Spain (BCN)').group(0)
'BCN'

Upvotes: 0

x10
x10

Reputation: 3834

No, this is good enough. You could make a function that accepts a string and returns it formatted.

Also, don't go with len(raw_text)-1, because that will fail on bad data like "Italy (BGN) ".

def get_code(str):
     return str[str.find('(')+1:str.find(')')]

formatted_text = get_code(raw_text)

Upvotes: 0

Related Questions