Strings basically just refer to all of the pieces of text used in Python aside from the actual code or commands themselves. For example, if we want to print someone's name, we would write their name as a string, as it isn't a functional part of the text but rather just a piece of text. The thing which turns a text into a string is the fact that it is written between quotation marks unlike regular code.
"Hello World!" # is a string
country = "Spain"
print(type(country))
Output:
<class 'str'>
We can put strings inside other strings by using single quotes and double quotes together. So if we want to have a string of a dialogue where we have to use double quotation marks inside of the string, we can just use single quotes when forming the string, and vice versa.
line = 'Emma asked him "What are you doing tonight?"'
# if we had put double quotation marks (" ") to form the string there would have been an error, a broken string.
In cases where we have to make a paragraph or another long text a string, we would use three quotation marks to form it.
Python has a cleaver function which allows you to receive data in the form of a string from the user. This is called the input function and it works just like the print function in the sense where you define it using parentheses. You write a prompt for the user within the parentheses and if you assign your input to a variable, that variable will be assigned whatever it is the user wrote when the prompt showed up.
For example, let's say we have an input asking for my age.
age = input("How old are you: ")
This prompt will be displayed and the user will have an opportunity to write their answer. If I type 15, then print age, the following will be printed:
"15"Â
Important note: Whatever is typed through an input function will be a string!
So although I typed 15 as an integer, since I typed it through the input function, it is set as "15" which is a string. If you wish to change the type of your data, you can always covert something to an integer:
age = input("How old are you: ")
new_age = int(age)
print(new_age) # will print 15, not "15"
There are many ways to format your strings, so to incorporate your variables into your strings. However, the easiest method is to use the concept of F-Strings. This is a method where you place the letter f in front of your string and open up curly parentheses wherever you want to incorporate a variable into your string. Here is what we mean:
name = "Jack"
job = "Software engineer"
print(f"His name is {name}, he is a {job}.")
Here, the text printed will be "His name is Jack, he is a Software engineer. If we change name to Michael and job to doctor, then the printed text will be "His name is Michael, he is a doctor.
Below, you can access Python's official documentation of string methods which include all string methods Python has to offer. Python has documentation on all sorts of things, so if you ever need help with a topic or are curious about it, you can check out the documentation of it.
Strings come with several different string methods, which allow us to modify our strings in cases where necessary. Python has lots of these string methods, however, below are the most handy ones:
string = "cake"
# count method - counts how many times something is in a string
count = string.count("a")
print(count) # Will print: 1
# lowercasing text - .lower() method
lower_string = string.lower()
print(lower_string) # Will print: cake
# uppercasing text - .upper() method
upper_string = string.upper()
print(upper_string) # Will print: CAKE
# capitalizing only the first letter of a string - .title()
capitalized = string.title()
print(capitalized) # Will print: Cake
# capitalizing the first letter of each word within the string - .capitalize()
string2 = "good morning"
capitalized = string2.capitalize()
print(capitalized) # Will print: Good Morning
print(string2.title()) # Will print: Good morning
# checking if a string is all capitalized - .isupper()
all_capitalized = string.isupper()
print(all_capitalized) # Will print: False
# checking if a string is all lowercase - .islower()
all_lowercase = string.islower()
print(all_lowercase) # Will print: True
# checking if a string is all numbers - isdigit()
all_numbers = string.isdigit()
print(all_numbers) # Will print: False
# checking if a string only contains letters or numbers - .isalnum()
letters_or_numbers = string.isalnum()
print(letters_or_numbers) # Will print: True
One particularly important one is the .len() method, which determines how many characters are in a string. This also includes spaces and punctuation.
print(len("Happy Birthday!"))
Output:
15
Please take note that this project covers not just strings, but all of the things you learned so far such as variables and mathematical operations.
Step 1: Make a variable which will receive and store data using the input regarding the job's title.
Step 2: Make a variable which will receive and store data using the input regarding the number of hours the job is.
Step 3: Make a variable which will receive and store data using the input regarding the hourly wage of the job.
Step 4: Make a variable which will receive and store data using the input regarding the how many kilometres transportation is to and back from work.
Step 5: Convert all data received via input that has to be in the form of an integer to the integer data type.
Step 6: Calculate and store the profit of a job.
Step 7: Calculate and store the profit of a job with transportation.
Step 8: Print a formatted string which states which jobs earns you how much with and without transportation.
Step 9: Try the code you have written on all jobs and see which is the most profitable.
Please note that this is just a suggested answer and any code which completes the same function is valid. To see which job is most profitable, you must try this code with all of the jobs in the project scenario.
job = input("Please enter the job: ").lower()
hours = int(input("Enter the hours per week: "))
wage = int(input("Enter the hourly wage in USD: "))
commute = int(input("How long is the commute to and back in KM: "))
profit = hours * wage
profit_with_commute = profit - (commute * 4) * 5
print(f"Your job of being a {job} earns you {profit} USD per week without, {profit_with_commute} USD per week with the commute.")
You have reached the end of Section 3: Strings!