Introduction
What are strings?
Well you already know, because you’ve already used them! Strings are a data type that can be used to store characters, numbers, symbols – pretty much anything!
How do we tell Python that we are creating a string?
Warning!
Don’t mix your speech marks and inverted commas ( ” and ‘ ) – Python will not like it 🙁
Advantages of strings
- Strings can store lots of different types of data, have a look at these different things stored in strings:
Disadvantages
- You can’t use maths functions with data stored in a string(see the demo)
- Strings can’t be changed – you have to overwrite them with a new string.
Joining Strings
Key Definition: Concatenation
Joining two or more strings together is known as string concatenation.
You already know how to join strings together, because you already did it in lesson 1!
Have a guess – How do we join strings together?
Example
String Methods
Strings have lots of useful methods that make your life easier when coding. Here are a few…
.capitalize()
This capitalizes the first the character in a string ( if it is a letter)
name = “bob”
print(name.capitalize())
Bob
[/python]
.title()
This capitalizes the first the character in a string ( if it is a letter)
name = “bob jones”
print(name.title())
Bob Jones
[/python]
.upper()
THIS CAPITALIZES EVERY LETTER IN THE STRING.
name = “bob”
print(name.upper())
BOB
[/python]
.lower()
this makes all the letters in the string lower case.
name = “bob”
print(name.lower())
bob
[/python]
.count(character)
This counts how many times a character appears in the string.
[python]
name = “bob”
print(name.count(“b”))
2
[/python]
.find(character)
Finds the position of the first instance of the character in the string. P.s. Python starts counting at zero, not 1!!!
[python]
greeting = “hello”
print(greeting.find(“o”))
4
[/python]
More methods…
Want to know more methods?
try doing : help(str) in the Interactive Interpreter to find out more string methods.
[/toggle]
Strings can't change
[note title=”Key Definition: Immutable”]
Any data type that cannot change is known as immutable.
[/note]
Strings, like many other data types, cannot be changed once you have created. This means that if you want to change any data stored in a string, you have to overwrite the whole variable with a new variable.
You don’t need to put the number in anything! e.g.
[python]
x = 5
[/python]
Once a number is stored in an integer, you can use any of the maths functions you would normally use.
[python]
x = 5
y = 7
z = x * y
print(z)
35
[/python]
If you want to store a data that contains a decimal place, such as math Pi, then you should normally use a float.
Just type the number, including the decimal place.
[python title=”Example”]
pi = 3.142
type(pi)
<class ‘float’>
[/python]
Floats are useful when you are doing calculations that require a higher level of accuracy in calculations.
[note title=”Clever Python 🙂 “]
Python 3 will automatically create a float when you need one.
[python]
x = 3
y = 2
z = x/y
1.5
type(z)
<class ‘float’>
[/python]
[/note]
int(value) - Convert to integer
Key Definition: Type Casting
Sometimes you will need to convert between data that is stored in different types. Python has in-built functions to do this for you.
Converting data from one type to another is known as Type Casting. Programmers often don’t bother using this term but examiners love testing students on obscure vocabulary!
[python]
x = “5”
type(x)
<class ‘str’>
x= int(x)
type(x)
<class ‘int’>
[/python]
Used for:
- Converting numbers that were inputted as a string.
- Throwing away decimal point information when we don’t need it (lazy!!!)
string(value) - Convert to string
Used for:
- Combining integer values and strings in to one string for printing or storage
Challenge 5 - Area of a Rectangle
Challenge 6 - Area of a circle
Challenge 7 - Number Generator (2 Digits)
Challenge 8 - Number Generator (3 Digits)
Challenge 9 - Atomic weight of Hydrocarbons
Challenge 10 - Standard Scratch
Challenge 11 - Formatting an address
Challenge 12 - Formatting telephone numbers
Attribution
These challenges we not made by me, but by Greg Reid.
http://community.computingatschool.org.uk/resources/1982 Licence
Have you…?
Have you…
- Completed the starter questions?
- Started challenges 1 – 4?
- Written down your homework? Challenges 5-12
Questions
Plenary Questions – add these to Socrative or develop in Quizmaster?
Joining 2 strings together is known as ____?
Data types that cannot be changed as known as ____?
Pi would be best stored as a ____?
A post code would be best stored as a ____?
An age you be best stored as a _____?
the input function stores all input as a ____?
When using the print statement with mixed data types is it best to use a comma or plus?
If you need to use the plus(+) in a print statement you should….?
Convert all strings to integers/floats
Convert all integers/floats to strings
Nothing, it will do it automatically