Download Strings - School of Information Technologies

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Python strings
Girls’ Programming Network
School of Information Technologies
University of Sydney
Mini-lecture 5
Strings
raw input
Types
Summary
2
Outline
1
Strings
2
Reading input with raw input
3
Types
4
Summary
Girls’ Programming Network
if statements
Mini-lecture 5
Strings
raw input
Types
Summary
3
Strings hold a sequence of characters
ˆ 'Hello World' is a string
ˆ We need to be able to distinguish the string from the program
ˆ Strings are delimited (a.k.a. separated) using quotes
ˆ You can use either single (') or double (") quotes
ˆ But they’ve got to match
ˆ Syntax highlighting in IDLE turns strings green
Girls’ Programming Network
if statements
Mini-lecture 5
Strings
raw input
Types
Summary
4
What happens when they don’t match?
ˆ Try entering this string "hello world' . . .
ˆ What about 'don't' or "he said "hi""?
ˆ There are two ways to solve these problem:
1 use the opposite quotes: "don't" or 'he said "hi"'
2 escape the quote: 'don\'t' or "he said \"hi\""
Girls’ Programming Network
if statements
Mini-lecture 5
Strings
raw input
Types
Summary
5
A string of digits is not a number!
ˆ Python stores different types of values
ˆ The string type (str) isn’t the same as the number type (int)
ˆ Try this case:
1
2
3
4
5
6
7
>>> 2*5
10
>>> "2"*"5"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'str'
>>>
Girls’ Programming Network
if statements
Mini-lecture 5
Strings
raw input
Types
Summary
6
But some arithmetic operators do work on strings
ˆ Addition of two strings is called concatenation
1
2
3
4
>>> 1 + 1
2
>>> "1" + '1'
'11'
ˆ Multiplication between strings and integers works too:
1
2
3
4
>>> "2"*5
'22222'
>>> 5*"2"
'22222'
ˆ Why does multiplication make sense?
Girls’ Programming Network
if statements
Mini-lecture 5
Strings
raw input
Types
Summary
7
Python provides code for reading from the user
ˆ All you need to do is supply a message:
1
2
3
4
5
>>> name = raw_input('Enter your name? ')
Enter your name? Jemima
>>> name
'Jemima'
>>>
ˆ raw_input is a Python builtin function
ˆ It takes a string argument to prompt the user with
ˆ Then it waits for input (just like the Python prompt does)
ˆ When the user presses Enter the input is returned
ˆ It is returned as a string and put in the name variable
Girls’ Programming Network
if statements
Mini-lecture 5
Strings
raw input
Types
Summary
8
Functions are named pieces of code that do something
ˆ When we refer to the code (followed by parentheses) it runs
ˆ This is called calling or invoking the function
1
2
3
>>> name = raw_input('Enter your name? ')
Enter your name? Jemima
>>>
ˆ Our example calls the raw_input function
ˆ raw_input goes purple because it is builtin
ˆ When a function needs some information to do its work:
ˆ we put the extra information inside the parentheses
ˆ these are called function arguments
Girls’ Programming Network
if statements
Mini-lecture 5
Strings
raw input
Types
Summary
9
Function calls are substituted by their answers
ˆ When a function call is finished:
ˆ the answer (or value the function was calculating) is returned
ˆ it effectively replaces the function call in an expression
ˆ just like a variable is replaced by its value in expressions
ˆ raw_input returns a string containing the input:
1
2
>>> name = raw_input('Enter your name? ')
Enter your name? Jemima
ˆ now this is the same as:
1
>>> name = 'Jemima'
ˆ because raw_input is effectively replaced by the string
'Jemima'
Girls’ Programming Network
if statements
Mini-lecture 5
Strings
raw input
Types
Summary
10
Converting between strings and integers
ˆ You need to explicitly convert between types
ˆ The Python int function converts values to integers:
1
2
3
>>> int('12345')
12345
>>>
ˆ Notice that there are no longer quotes around 12345
ˆ The Python str function converts values to strings:
1
2
3
>>> str(12345)
'12345'
>>>
ˆ This time the quotes have been added because it is now a
string
Girls’ Programming Network
if statements
Mini-lecture 5
Strings
raw input
Types
Summary
11
raw input and int
ˆ Often you want to read an integer using raw_input
ˆ But raw_input only gives back strings
ˆ int to the rescue:
1
2
3
4
5
6
7
8
9
>>> num
Enter a
>>> num
'5 '
>>> num
Enter a
>>> num
5
>>>
Girls’ Programming Network
= raw_input('Enter a number? ')
number? 5
= int(raw_input('Enter a number? '))
number? 5
if statements
Mini-lecture 5
Strings
raw input
Types
Summary
12
You should now be able to:
ˆ Create Python strings
ˆ Call Python functions
ˆ Read input from the user with raw_input
ˆ Convert between integers and strings using str or int
Girls’ Programming Network
if statements
Mini-lecture 5