Python documentation: https://docs.python.org/3/
Variables & Strings
- Introduction to the objects string & list with their different attributes
- Using the shell
A string is a chain of characters / text and can contain any type of characters.
A string is defined by ” “
Write text using string
>>> print("La Cambre")
Exercise: Write your name
>>> print("Brussels"+"Paris") >>> print("Brussels "+"Paris")
Exercise: Write your address
Composing a sentence
>>> print("Paris", "to", "London", "via", "Brussels") >>> print("Paris to London via Brussels")
Exercise: Write your favourite expression
>>> print(3*3) >>> print(3 * "algolit" + " in Brussels")
Exercise: Write 'I write the alphabet' 3 times.
Note: there are always different possible solutions.
Changing from Python in the shell to the editor: https://www.anaisberck.be/python-editor/
Write a string as a variable
This avoids having to retype your string each time you use it You can change values at any time of the writing process.
letter = "a" print(letter) word = "Python" print(word) sentence = "I learn to read and write again in Python." print(sentence, letter)
Exercise: Print your letter, word, sentence
print(letter + " " + word + " " + sentence + ".") print(letter + "! " + word + "? " + sentence + ".") letter = "i" print letter + "! " + word + "? " + sentence + "."
Exercise: change content of one of variables, over and over. See how result changes
Calculate the length of a string
print(len(letter)) print(len(word)) print(len(sentence)) print(len(word))+2)
a_number = len(word)+2 print(a_number) a_number += 3 print(a_number)
Exercise: Compose a sentence word by word, specifying each word as a variable. The length of the sentence is 20.
Letters/words/sentences as fields or grids
Each letter occupies a specific position. We can access strings using indexes.
Note: computer starts to count from 0
Exercise: What is the middle letter of your sentence?
Slicing and concatenating strings
in order to select letters & compose new words
- Find last letter
- Find last but one letter
- Find first two letters
- Find 3 letters in the middle
- Find from 3rd letter till end
Exercise: If the word is "solidarity", what do you read here? word[:5] + word[3:] Exercise: rewrite the word as 'liquidity', using slicing
replace()function is another function which can be called on a string. It will replace all occurrences of a specified substring with another string.
text = "Joe Biden won the elections." text = text.replace(" the elections", "") print(text) text = text.replace("Joe Biden", "Donald Trump") print(text)
Exercise: text = "Research shows that it is possible to understand text when all vowels are removed"
lower() converts a string to lowercase characters and
upper() returns an uppercased version.
Write first word of sentence with capital letter”
Note the difference
What you’ve learned
- variable - value - assignment operator (=) - difference between variables and values - integers (numbers) - print() - len() - slicing and concatenating - replace() - lower() - upper() - capitalize() - title()