Skip to content
Learnearn.uk » Python Unit Home » Python Data Types

Python Data Types

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!


myStr = “Hello!”

 

How do we tell Python that we are creating a string?

Answer
We put the data inside speech marks or inverted commas!

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.
Demo

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?

Answer
We used the + sign!

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)

“Example”

name = “bob”

print(name.capitalize())

Bob

[/python]

.title()

This capitalizes the first the character in a string ( if it is a letter)

“Example”

name = “bob jones”

print(name.title())

Bob Jones

[/python]

.upper()

THIS CAPITALIZES EVERY LETTER IN THE STRING.

“Example”

name = “bob”

print(name.upper())

BOB

[/python]

.lower()

this makes all the letters in the string lower case.

“Example”

name = “bob”

print(name.lower())

bob

[/python]

.count(character)

This counts how many times a character appears in the string.

Example

[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!!!

Example

[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.

Demo




If you are looking to store whole numbers ( positive or negative) then you will probably usually want to store in them in the integer data type. Doing this is easy, in fact you have already done this!


myInt = 55

Answer

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.

Example

[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.


myFlt = 3.412

Answer

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!


myInt = int(“45”)

[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!!!)
Demo

string(value) - Convert to string

Used for:

  • Combining integer values and strings in to one string for printing or storage

Demo


Challenge 5 - Area of a Rectangle

ch5

Challenge 6 - Area of a circle

ch6

Challenge 7 - Number Generator (2 Digits)

ch7

Challenge 8 - Number Generator (3 Digits)

ch8

Challenge 9 - Atomic weight of Hydrocarbons

ch9

Challenge 10 - Standard Scratch

ch10

Challenge 11 - Formatting an address

c11

Challenge 12 - Formatting telephone numbers

ch12

Attribution

These challenges we not made by me, but by Greg Reid.

http://community.computingatschool.org.uk/resources/1982   Licence

Have you…?checklist2

Have you…

  • Completed the starter questions?
  • Started challenges 1 – 4?
  • Written down your homework? Challenges 5-12
QuizMaster

 

Let's learn Python YouTube Video - Data Types

Let’s learn Python YouTube Video – Data Types

 

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