Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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