Members Online
Total Online: 32 Web Spiders: 15
Guests Online: 31
Members Online: 1
Registered Members: 70209 Newest Member: KalareShou
|
View Thread
| Author |
Python pulling info |
Cr4zyM4n123
Member
Posts: 8
Location: 127.0.01
Joined: 20.02.10 Rank: Monster |
|
Hello everyone, I am new here to HBH, and have recently started learning Python. It is really relatively simple to learn, maybe because I already have some background in PHP, HTML, CSS, and Javascblockedript, but I was wondering if someone could help me out here.
My question is how do I pull out out a certain string, input(ed) by a user and make it come out as something else. This is not very clear, maybe if you see some of my code it will make more sense
a = 1 #obviously the letter 'a' is the variable which is equivalent to 1
use = raw_input("Please enter the letter 'a': "); #user inputs a letter in this case 'a'
if use == 'a':
print a;
Okay, as most of you who do any type of Python, or perhaps JAVA and C++ programming may know this code will allow a user to input a string as long as it is 'a', it will output 1. Now, what I would like to be able to do is have it equal grab the letter 'a' out of the text the user submits and automatically print a. I thought about doing this with a Function, but I am not quite exactly sure how to go about this. If this is still unclear, please let me know and I will try harder to make it a bit more comprehensible.
Thanks |
|
| Author |
RE: Python pulling info |
stealth-
Member

Posts: 999
Location: Eh?
Joined: 10.04.09 Rank: God |
|
First off, Welcome to HBH :)
Secondly, I'm not sure what *exactly* your trying to accomplish, however I came up with a few ideas of what you might be trying to do.
If you are trying to get it to print the 1st letter of whatever is inputted by the user (a, b, c, no matter what it is) you can use indices.
variable = raw_input("Enter the letter 'a': ")
print variable[0]
If the user enters 'a', that will print 'a'. If the user enters 'apples', that will print 'a'. If the user enters 'carrots', that will print 'c'.
Another thing I thought you might be trying to do is find 'a' anywhere in the string and if it's there, print 1. This can be done though the string's find function
a = 1
variable = raw_input("Enter the letter 'a': ")
if variable.find('a') != -1:
print a
If the user enters 'a', it will print 1. If the user enters 'carrots', it will print 1. If the user enters 'bog', it won't print anything. If that's what you were looking for I can further evaluate how that works.
Hope that helps :)
If not, please explain a little better.
The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealth-x.com
Edited by stealth- on 21-02-10 07:26 |
|
| Author |
RE: Python pulling info |
Cr4zyM4n123
Member
Posts: 8
Location: 127.0.01
Joined: 20.02.10 Rank: Monster |
|
Thanks for the welcome and a lot for the code. I think the second line(s) of code is more of what I am looking for thank you =) I am just learning Python lol it's only been a few days, but I think I'm starting to get a grasp on it lol.
Thanks again for the code |
|
| Author |
RE: Python pulling info |
stealth-
Member

Posts: 999
Location: Eh?
Joined: 10.04.09 Rank: God |
|
Anytime :)
In case you were wondering, find() is a function of a string. Therefore, this will work on every string you can come up with, but only strings. You give find() a string that you want it to look for, in our case 'a', and it returns a integer which is the location of the first occurrence of that string. If it does not find the string, it will return -1.
So find() can be used for other things, such as telling where *exactly* they entered 'a'. Like this:
variable = raw_input("Enter a phrase containing 'a': ")
location = variable.find("a")
if location == -1:
print "That phrase doesn't contain a 'a'!"
else:
print "The first occurrence of 'a' was at:"
print location
Keep in mind though that python starts counting at 0. So if you entered "cat", it would return a 1, not a 2, like you would expect.
Feel free to PM me if you need any more help :)
The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealth-x.com
Edited by stealth- on 21-02-10 21:00 |
|
| Author |
RE: Python pulling info |
michelfalke
Member
Posts: 9
Location: Longitude l = new Longitude();
Joined: 08.01.10 Rank: HBH Guru |
|
This is a link i personally used to kick off in python it thought me the basics of how python works 
http://www.dickbaldwin.com/tocpyth.htm
Don't know if it's any use for u.
School sux hbh teaches me way more =) |
|
| Author |
RE: Python pulling info |
Cr4zyM4n123
Member
Posts: 8
Location: 127.0.01
Joined: 20.02.10 Rank: Monster |
|
Thanks again, to the both of you =) I will be sure to check out that link ! and I will most likely take you up on the PM offer stealth- =P I'll try not to annoy you with a ton of questions LOL
|
|
| Author |
RE: Python pulling info |
stealth-
Member

Posts: 999
Location: Eh?
Joined: 10.04.09 Rank: God |
|
|
Cr4zyM4n123 wrote:
Thanks again, to the both of you =) I will be sure to check out that link ! and I will most likely take you up on the PM offer stealth- =P I'll try not to annoy you with a ton of questions LOL
Heh, send as many as you'd like. I enjoy helping people with python stuff 
The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealth-x.com |
|
| Author |
RE: Python pulling info |
techb
Member

Posts: 384
Location:
Joined: 15.02.09 Rank: Hacker Level 2 |
|
it looks like i'm posting in this thread a little late, but i figured i'd throw in my two cents.
instead of find() you could also use index()
v = raw_input("Enter something with 'a': "
try:
print "'a' found at %d"
except: print "'a' not found"
i used index() ALLOT when i was reading pixels from my webcam.
oh and i'm also new to hbh, what tags do i use to put code in?
Edited by techb on 23-02-10 07:03 |
|
| Author |
RE: Python pulling info |
stealth-
Member

Posts: 999
Location: Eh?
Joined: 10.04.09 Rank: God |
|
techb wrote:
it looks like i'm posting in this thread a little late, but i figured i'd throw in my two cents.
instead of find() you could also use index()
v = raw_input("Enter something with 'a': " 
try:
print "'a' found at %d"
except: print "'a' not found"
i used index() ALLOT when i was reading pixels from my webcam.
oh and i'm also new to hbh, what tags do i use to put code in?
Nice, I'd forgotten about that 
You can use the [co'de] and [/co'de] tags. Just ignore the ' I put in each tag to avoid it actually showing up in code tags. There are links right below the response window for common things like quotes, imgs, centered, and so on.
The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealth-x.com |
|
|
|
|