Опубликован: 12.07.2013 | Уровень: специалист | Доступ: платный
Лекция 3:

Strings

< Лекция 2 || Лекция 3: 12 || Лекция 4 >
Аннотация: That's enough of integers and math for now. Python is more than just a calculator. Now let's see what Python can do with text. In this chapter, we will learn how to store text in variables, combine text together, and display them on the screen.

Topics Covered In This Chapter:

  • Flow of execution
  • Strings
  • String concatenation
  • Data types (such as strings or integers)
  • Using IDLE to write source code.
  • Saving and running programs in IDLE.
  • The print() function.
  • The input() function.
  • Comments
  • Capitalizing variables
  • Case-sensitivity
  • Overwriting variables

That's enough of integers and math for now. Python is more than just a calculator. Now let's see what Python can do with text. In this chapter, we will learn how to store text in variables, combine text together, and display them on the screen. Many of our programs will use text to display our games to the player, and the player will enter text into our programs through the keyboard. We will also make our first program, which greets the user with the text, "Hello World!" and asks for the user's name.

Strings

In Python, we work with little chunks of text called strings. We can store string values inside variables just like we can store number values inside variables. When we type strings, we put them in between two single quotes ('), like this:

>>> spam = 'hello' 
>>>

The single quotes are there only to tell the computer where the string begins and ends (and is not part of the string value).

Now, if you type spam into the shell, you should see the contents of the spam variable (the 'hello' string.) This is because Python will evaluate a variable to the value stored inside the variable.

>>> spam = 'hello'
>>> spam
'hello'
>>>

Strings can have almost any character or sign in them as well as spaces and numbers. (Strings can't have single quotes inside of them without using an escape character. Escape characters are described later.) These are all examples of strings:

'hello'
'Hi there!'
'Albert'
'KITTENS'
'7 apples, 14 oranges, 3 lemons'
'A long time ago in a galaxy far, far away...'
'O*&#wY%*&OCfsdYO*&gfC%YO*&%3yc8r2'

As we did with numerical values in the previous chapter, we can also put string values in expressions. For example, the expression 4 * 2 + 3 is an expression with numerical values that will evaluate to the integer 11.

String Concatenation

You can add one string to the end of another by using the + operator, which is called string concatenation. Try entering 'Hello' + 'World!' into the shell:

>>> 'Hello' + 'World!'
'HelloWorld!'
>>>

To keep the strings separate, put a space at the end of the 'Hello' string, before the single quote, like this:

>>> 'Hello ' + 'World!'
'Hello World!'
>>>

Strings and integers are different data types. All values have a data type. The data type of the value 'Hello' is a string. The data type of the value 5 is an integer. The data type of the data that tells us (and the computer) what kind of data it is.

Writing Programs in IDLE's File Editor

Until now we have been typing instructions one at a time into the interactive shell. When we write programs though, we type in several instructions and have them run all at once. Let's write our first program!

The name of the program that provides the interactive shell is called IDLE, the Interactive DeveLopement Environment. IDLE also has another part called the file editor.

Click on the File menu at the top of the Python Shell window, and select New Window. A new blank window will appear for us to type our program in. This window is the file editor.

The file editor window.

Рис. 3.1. The file editor window.

Hello World!

The bottom right of the file editor window tells you where the cursor number is. The cursor is currently on line 12.

Рис. 3.2. The bottom right of the file editor window tells you where the cursor number is. The cursor is currently on line 12.

A tradition for programmers learning a new language is to make their first program display the text "Hello world!" on the screen. We'll create our own Hello World program now.

When you enter your program, don't enter the numbers at the left side of the code. They're there so we can refer to each line by number in our explanation. If you look at the bottom-right corner of the file editor window, it will tell you which line the cursor is currently on.

IMPORTANT NOTE! The following program should be run by the Python 3 interpreter, not the Python 2.6 (or any other 2.x version). Be sure that you have the correct version of Python installed. (If you already have Python 2 installed, you can have Python 3 installed at the same time.) To download Python 3, go to http://python.org/download/releases/3.1.1/ and install this version.

Enter the following text into the new file editor window. We call this text the program's source code because it contains the instructions that Python will follow to determine exactly how the program should behave. (Remember, don't type in the line numbers!)

hello. py

This code can be downloaded from http://inventwithpyt.hon.com/hello.py

If you get errors after typing this code in, compare it to the book's code with the online

diff tool at http://inventwithpython.com/diff or email the author at

al@inventwithpython.com

1. # This program says hello and asks for my name.
2. print('Hello world!')
3. print('What is your name?')
4. myName = input ()
5. print('It is good to meet you, ' + myName)

The IDLE program will give different types of instructions different colors. After you are done typing this code in, the window should look like this:

The file editor window will look like this after you type in the code.

Рис. 3.3. The file editor window will look like this after you type in the code.
< Лекция 2 || Лекция 3: 12 || Лекция 4 >
Марат Хасьянов
Марат Хасьянов
Россия
Роман Дрындик
Роман Дрындик
Россия