473,545 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Selecting first word of a variable

4 New Member
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 2739
bartonc
6,596 Recognized Expert Expert
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
Boblert
4 New Member
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 Recognized Expert Contributor
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
Boblert
4 New Member
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 Recognized Expert Contributor
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
Boblert
4 New Member
Thanks alot thats solved my problem :D
Aug 19 '07 #7
bartonc
6,596 Recognized Expert Expert
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 Recognized Expert Contributor
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 Recognized Expert Expert
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

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

Similar topics

5
1746
by: Steven Bethard | last post by:
I have a list of dictionaries. Each dictionary holds counts of various 'words', e.g.: py> countdicts = I need to select dicts with the constraint that the number of each 'word' totalled over all selected dicts doesn't exceed a given MAX_VALUE. Right now, I do this by: py> def select(dicts, n):
5
3341
by: Axial | last post by:
Question: How to select columns from Excel-generated XML when some cells are empty. I've found examples where rows are to be selected, but I can't seem to extrapolate from that to selecting columns when some cells are empty. Is there a way to use the ss:Index to account for the missing <Cell elements? Thank you for any suggestions. ...
18
5678
by: booner | last post by:
I have a form that when it loads I would like to highlight the values (from a DB) that have been selected in a multiple selection list (<select multiple="true">. function onLoad() { document.forms.elements.value = "<value from DB>"; }
3
4249
by: phade2blue | last post by:
Hi guys and gals, I am new to db and trying to select into a variable from a table. I am using the code: - DoCmd.RunSQL "SELECT """ & variable & """ = column1 " & _ "FROM table WHERE column1 =""" & another_variable & """" but not having a lot of look. Am doing something drastically wrong?
1
9281
by: Cheryl | last post by:
I have problems selecting the attribute when I use the SelectNode function. <book> <title store='7456' >Nutshell</title> <author id='1'>Drayton</author> <xauthor publish = "1" publishtime = "2"/> <author id='3'>Neward</author> <author id='2'>Albahari</author> </book>
3
1812
by: requeth | last post by:
Allo, I'm a little confused. I have an XSLT, in which my parser selects a node, but I want to just capture the first two characters to a variable. How would I do this? I know how to create the variable, but I'm not sure how to take just the first two characters of the string. Thanks, RT Starid
8
5002
by: John Barleycorn | last post by:
Hi I'm sorry if this question is a bit basic for most people, but I'm really new to VB (I'm using the .NET 2005 Express Edition), but I've set myself a task and would love to see it through. The problem is that an application we use has a config file which looks XML-like and contains the following: <design filerevision="1"...
4
3680
by: visu | last post by:
I need a solution to my problem. the problem is I ll have a button in page ... and when i click it .. content of a div tag has to be get selected (i.e what we normally do with mouse to selecting some part of a page).. then i can do CTRl+C to copy the selected div content.. to paste it in MS Word or in any other external application..
2
2649
by: IcedDante | last post by:
Working with a sorted group, the inability to use following-sibling (which uses Document Order) and convert an RTF (not avaible with the Parser that we are using) hampered our ability to solve the following problem. Consider the following set of Data: <example> <Credentials time="3"> <UserId>kiss</UserId> </Credentials> <Credentials...
0
7453
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7390
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
5958
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5318
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4940
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1865
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1007
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.