5

Is it possible to use raw_input with a variable?

For example.

max = 100
value = raw_input('Please enter a value between 10 and' max 'for percentage')

Thanks,

Favolas

2
  • 2
    This fundamentally has nothing to do with the raw_input function and everything to do with getting a variable to appear as part of a string. Commented Dec 5, 2010 at 0:53
  • 1
    English is not my language so it is difficult to explain me better. You are right Karl. Commented Dec 5, 2010 at 23:06

4 Answers 4

10

You can pass anything that evaluates to a string as a parameter:

value = raw_input('Please enter a value between 10 and' + str(max) + 'for percentage')

use + to concatenate string objects. You also need to explicitely turn non-strings into string to concatenate them using the str() function.

Sign up to request clarification or add additional context in comments.

Comments

2

Python Language Reference, §5.6.2, "String Formatting Operations"

Comments

1

i think this might work

value = input('Please enter a value between 10 and {} for percentage'.format(max))

Comments

1

one more way to do it :)

value = raw_input('Please enter a value between 10 and %i for percentage' % (max))

1 Comment

This might be better as a comment on someone else's answer, but if you want to answer the question you could try to let your answer to stand on its own and add examples for the different ways, you also might want to mention Python 3's new string interpolation features

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.