Python pulling info
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 JavaScript, 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
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.
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
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.
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:
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 :)
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.
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?
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.