alex123
alex123

Reputation: 25

Return first word of the string

I has a data like below:

Colindale London                                        
London Borough of Bromley                               
Crystal Palace, London                                  
Bermondsey, London                                      
Camden, London  

This is my code:

def clean_whitespace(s):
    out = str(s).replace(' ', '')
    return out.lower()

My code now just return the string that has been remove white space. How can I select the first word the the string. For example:

Crystal Palace, London -> crystal-palace                             
Bermondsey, London -> bermondsey                                      
Camden, London -> camden

Upvotes: 0

Views: 82

Answers (2)

Abhishek Kulkarni
Abhishek Kulkarni

Reputation: 1767

Try this below :

s =  "Crystal Palace, London"
output = s.split(',')[0].replace(' ', '-').lower()
print(output)

Upvotes: 1

huy
huy

Reputation: 1914

You can try this code:

s = 'Bermondsey, London'

def clean_whitespace(s):
    out = str(s).split(',', 1)[0]
    out = out.strip()
    out = out.replace(' ', '-')
    return out.lower()

print(clean_whitespace(s))

Output:

bermondsey

Upvotes: 2

Related Questions