Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Getting String Input

Before you can finally write “real programs,” you’ll need to be able to write scripts that can query the user for more information. Fortunately, Python includes a powerful built-in function, the input function, which makes this easy.

I’ll give you the syntax first and then show examples.

key.jpg
string_var = input(prompt_string)

The essence of this syntax is that the built-in input statement both takes and produces a text string. In one way, the concept of text string is easy to understand; it’s just “words” for the most part—or more accurately, letters and other characters.

For example, we might write a function, main, which we’re going to use as a script.

>>>def main():
    name1_str = input(’Enter your name: ’)
    name2_str = input(’Enter another: ’)
    name3_str = input(’And another: ’)
    print(’Here are all the candidates: ’)
    print(name1_str, name2_str, name3_str)

>>>main()
Enter your name: Brian
Enter another: Hillary
And another: Donald
Here are all the candidates:
Brian Hillary Donald

This by itself is not a very exciting program. It takes some text and displays it on the console. But this little program demonstrates an important ability of Python: the ability to prompt the user for a text string and then assign it to a variable.

While other variables up until now have referred to numeric values, these variables—name1, name2, and name3—all refer to text strings in this case.

What exactly can go into a text string? Basically, anything you can type can go in a text string. Here’s an example:

>>>in_str = input(’Enter input line: ’)
Enter input line: When I’m 64...
>>>in_str
’When I’m 64...’

As you can see, text strings can contain numerals (digit characters). But until they’re converted, they’re just numerals. They are text-string representations of numbers, not numbers you can perform arithmetic on.

If this isn’t obvious, just remember that the numeral 5 is just a character on a keyboard or on the screen. But the number 5 can be doubled or tripled to produce 10 or 15 and has little to do with characters on a keyboard.

Here’s an example:

in_str = ’55’

But assigning 55 with no quote marks around it does something different.

n = 55

The difference is that 55 is an actual number, meaning that you can add, subtract, multiply, and divide it. But when enclosed in quotation marks, ’55’ is a text string. That means it is a string consisting of two numerals, each a 5, strung together.

A simple program should help illustrate the difference.

>>>def main():
    in_str = input(’Enter your age: ’)
    print (’Next year you’ll be’, in_str + 1)

>>>main()
Enter your age: 29
Error! Incompatible types.

Oops! What happened? The characters 29 were entered at the prompt and stored as a text string, that is, a numeral 2 followed by a numeral 9—a string two characters long. But that’s not the same as a number, even though it looks like one.

Python complains as soon as you try to add a text string to a number.

in_str + 1

No error is reported until you execute the function. Python variables don’t have types; only data objects do. Consequently, Python syntax seems lax at first. But the types of data objects—which are not checked for syntax errors, as there are no “declarations”—are checked whenever a Python statement is actually executed.

This means, among other things, that you cannot perform arithmetic on a string of numerals such as 100, until that string is first converted to numeric format. If this doesn’t make sense now, don’t worry; it will make sense when you read the next section.

The next section shows how to get input and store it as a number rather than text string.

  • + Share This
  • 🔖 Save To Your Account