A function is something that takes something in, processes it and gives us an output.
Python’s print is a function which takes in a string from the user and displays it on the screen.
A function in Python is normally written as the name followed by brackets with any inputs (arguments) to the function inside the brackets:
print("Hello world")
String Functions: to uppercase
Strings functions work differently. They are instead chained to the end of a variable name:
myname = "John"
print(myname.upper())
Function readability
Python, like many other programming languages, aims to make function names as readable as possible.
For instance, isupper() is designed to check if a string is entirely uppercase whereas isnumeric() is used to check if the string is entirely numeric.
myname = "John"
print(myname.upper())
String functions: substring
We can also obtain a substring very easily in Python. This done with the index, which starts at 0. We use the [ ] to access a specific index.
We can also obtain several characters in Python in the same way, we just need to specify the range we want within the brackets:
first_name = "Joseph"
print(first_name[1])
first_name = "Joseph"
print(first_name[0:3])
Complete the worksheet.
For the first part, you will need to write what you expect to come out of the function and then test functions using Python and write what you get.