[Tutor] Finding and replacing a space in a string.

Peter Brown peterabrown at froggy.com.au
Wed Jan 28 16:54:07 EST 2004

At 16:36 28/01/04 -0500, you wrote:
>Hey guys and gals...
>I'm working with the csv module...I have a row[1] which is a ID.
>let's call  ID = row[1].
>My problem is that row[1] or ID is  6 characters or less and in some 
>casses it has a '  '  space which i need to replace with a '-' and if  it 
>does not have a space I would like to leave it alone.
>Any sugestions ?
>for example:
> >>> id
>newid  should become 'ZURE-A'
>and if  id = 'WHITO'  newid = 'WHITO'
 >>> id ='ZUE A'
 >>> id1='ZUREA'
 >>> id1= id1.replace(' ','-')
 >>> id1
 >>> id= id.replace(' ','-')
 >>> id
In other words if id doesn't have a space it doesn't treat the command as 
an error. So you are free to apply to every line, where there is a space it 
will replace it, otherwise id is not chnaged.


>Thanks for the help :)
>Learning python and loving it !
>Tutor maillist  -  Tutor at python.org

Peter Brown

More information about the Tutor mailing list