472,101 Members | 1,406 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,101 software developers and data experts.

Selecting first word of a variable

How do I select the first word of a variable?
Expand|Select|Wrap|Line Numbers
  1. name = raw_input("What's your full name? ")
  2. print "Hey, %s, how you doing?" % name
So say I only wanted it to print the first name? How would I do this not knowing the amount of letters in it?
Aug 19 '07 #1
9 2682
bartonc
6,596 Expert 4TB
How do I select the first word of a variable?
Expand|Select|Wrap|Line Numbers
  1. name = raw_input("What's your full name? ")
  2. print "Hey, %s, how you doing?" % name
So say I only wanted it to print the first name? How would I do this not knowing the amount of letters in it?
Expand|Select|Wrap|Line Numbers
  1. >>> name = "Jo Shcmo"
  2. >>> firstandlast = name.split()
  3. >>> firstname = firstandlast[0]
  4. >>> firstname
  5. 'Jo'
  6. >>> 
Aug 19 '07 #2
Thanks alot,
also how would I create a function to replace but only if its the whole words for example:

Replace:
man : guy

Sentence to be replaced:
That man is good but he ain't no superman.

But I can only get it to replace as:
That guy is good but he ain't no superguy.

But I want it to replace as:
The guy is good but he ain't no superman.


Sorry if that my problem is confusing its hard to explain
Aug 19 '07 #3
ilikepython
844 Expert 512MB
Thanks alot,
also how would I create a function to replace but only if its the whole words for example:

Replace:
man : guy

Sentence to be replaced:
That man is good but he ain't no superman.

But I can only get it to replace as:
That guy is good but he ain't no superguy.

But I want it to replace as:
The guy is good but he ain't no superman.


Sorry if that my problem is confusing its hard to explain
Try something like this:
Expand|Select|Wrap|Line Numbers
  1. sent = "That man is good but he ain't no superman."
  2. words = sent.split()
  3.  
  4. for (i, word) in enumerate(words):
  5.     if word == "man":
  6.         words[i] = "guy"
  7.  
  8. newsent = " ".join(words)
  9.  
You could also use the re module.
Aug 19 '07 #4
Ye, thats what I've been doing. But it is a large word-replace list and large paragraph.
Just thought there might of been a way with less lines used :D
and I don't quite understand the re module
Aug 19 '07 #5
ilikepython
844 Expert 512MB
Ye, thats what I've been doing. But it is a large word-replace list and large paragraph.
Just thought there might of been a way with less lines used :D
and I don't quite understand the re module
If you have more than one word you can do something like this (don't know if you did it already) ;):
Expand|Select|Wrap|Line Numbers
  1. replacedict = {"man": "guy", "woman": "girl", "car": "automobile"} # etc...
  2.  
  3. for i in range(len(paragraph)):
  4.     if paragraph[i] in replacedict.keys():
  5.         paragraph[i] = replacedict[paragraph[i]]
  6.  
Technically, that uses the same amount of lines.
Aug 19 '07 #6
Thanks alot thats solved my problem :D
Aug 19 '07 #7
bartonc
6,596 Expert 4TB
Ye, thats what I've been doing. But it is a large word-replace list and large paragraph.
Just thought there might of been a way with less lines used :D
and I don't quite understand the re module
A Regular Expression (re), has it's own language. A VERY simple example would be a sub-string followed by any whitespace (pure Python could easily do this to, but "regex"s, as they are called go much further):
Expand|Select|Wrap|Line Numbers
  1. >>> import re
  2. >>> s = "That man is good but he ain't no superman"
  3. >>> reObj = re.compile("man\s")
  4. >>> reObj.sub("guy ", s)
  5. "That guy is good but he ain't no superman"
  6. >>> 
Just thought I'd throw that out there.
Aug 20 '07 #8
ghostdog74
511 Expert 256MB
Thanks alot,
also how would I create a function to replace but only if its the whole words for example:

Replace:
man : guy

Sentence to be replaced:
That man is good but he ain't no superman.

But I can only get it to replace as:
That guy is good but he ain't no superguy.

But I want it to replace as:
The guy is good but he ain't no superman.


Sorry if that my problem is confusing its hard to explain
read the docs for replace() more carefully. The docs are your good friend.
Expand|Select|Wrap|Line Numbers
  1. >>> "This man is a superman".replace("man","guy",1)
  2. 'This guy is a superman'
  3. >>> "This man is a superman".replace("man","guy")
  4. 'This guy is a superguy'
  5. >>> 
  6.  
Aug 20 '07 #9
bartonc
6,596 Expert 4TB
read the docs for replace() more carefully. The docs are your good friend.
Expand|Select|Wrap|Line Numbers
  1. >>> "This man is a superman".replace("man","guy",1)
  2. 'This guy is a superman'
  3. >>> "This man is a superman".replace("man","guy")
  4. 'This guy is a superguy'
  5. >>> 
  6.  
"superguy", that's a good one!
Aug 20 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Steven Bethard | last post: by
5 posts views Thread by Axial | last post: by
18 posts views Thread by booner | last post: by
3 posts views Thread by phade2blue | last post: by
3 posts views Thread by requeth | last post: by
8 posts views Thread by John Barleycorn | last post: by
2 posts views Thread by IcedDante | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.