By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,862 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

Selecting first word of a variable

P: 4
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
Share this Question
Share on Google+
9 Replies


bartonc
Expert 5K+
P: 6,596
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

P: 4
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
Expert 100+
P: 844
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

P: 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
Expert 100+
P: 844
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

P: 4
Thanks alot thats solved my problem :D
Aug 19 '07 #7

bartonc
Expert 5K+
P: 6,596
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

Expert 100+
P: 511
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
Expert 5K+
P: 6,596
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.