Support Forums

Full Version: Need help with python variables and raw_input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I there a way I can do something like:
raw_input("Say something: ")
The person would then type:
Hello
Is there a way to make
x= the first letter(in this case h)
y= the second letter(in this case e)
And so on...?
Yes
try

Code:
t = raw_input("Type: ")
print t[0]
Yep the string slices:

x = raw_input('Say something: ')

Then we have variable x with string 'Hello'.

Code:
print x[0:1]
'H'
Code:
print x[1:2]
'e'
Code:
print x [2:3]
'l'

If you then want variable y to be the second letter:

Code:
y = x[1:2]