Skip to content
Learnearn.uk » Home » Python Strings

Python Strings

Strings

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!

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)

String Immutability

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.

 

Concatenation

String Concatenation

Joining two or more strings together is known as string concatenation.

 

String Methods

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

.title()

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

name = "bob jones"

print(name.title())

Bob Jones

.upper()

THIS CAPITALIZES EVERY LETTER IN THE STRING.

name = "bob"

print(name.upper())

BOB

.lower()

this makes all the letters in the string lower case.

name = "bob"

print(name.lower())

bob

.count(character)

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

name = "bob"

print(name.count("b"))

2

.find(character)

Finds the position of the first instance of the character in the string. P.s. Python starts counting at zero, not 1!!!

greeting = "hello"

print(greeting.find("o"))

4

More methods…

Want to know more methods?

try doing : help(str) in the Interactive Interpreter to find out more string methods.

String to list

Strings challenges