Strings

Python has a built in string type, supporting many useful methods.

given = "John"
family = "Doe"
full = given + " " + family

John Doe

So + for strings means “join them together” - concatenate.

Other useful methods include:

print(full.upper())

JOHN DOE

As for float and int, the name of a type can be used as a function to convert between types:

ten, one

(10,1)

print(ten + one)

11

For example, can then convert to a string, before converting back to float:

print(float(str(ten) + str(one)))

101.0

We can remove extraneous material from the start and end of a string:

"    Hello  ".strip()

‘Hello’

Note that you can write strings in Python using either single (‘ … ‘) or double (“ … “) quote marks. The two ways are equivalent. However, if your string includes a single quote (e.g. an apostrophe), you should use double quotes to surround it:

"John's Class"

“John’s Class”

And vice versa: if your string has a double quote inside it, you should wrap the whole string in single quotes.

'"Wow!", said Bob.'

‘“Wow!”, said Bob.’

Next: Reading - Lists, Sequences and unpacking