Literary Python for Beginners

Next pages: Python editor // Collections // Conditions // Loops // Working with text files

Inspiration: https://github.com/mikekestemont/ghent1516

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

Add text

>>> 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

Multiply

>>> 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

Add punctuation

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!

Calculate the length of a string

print(len(letter)) 
print(len(word)) 
print(len(sentence))
print(len(word))+2) 

And more

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

print(word[0])
print(word[3])
Exercise: What is the middle letter of your sentence?

Slicing and concatenating strings

in order to select letters & compose new words

  • Find last letter
print(word[-1])
  • Find last but one letter
print(word[-2])
  • Find first two letters
print(word[0:2])

or

print(word[:2])
  • Find 3 letters in the middle
print(word[2:5])
  • Find from 3rd letter till end
print(word[2:])
Exercise: If the word is "solidarity" is, what do you read here?
word[:5] + word[3:]

Exercise: rewrite the word as 'liquidity', using slicing

replace()

The 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"

Change case

lower() converts a string to lowercase characters and upper() returns an uppercased version.

print(letter.lower())
print(sentence.upper())

Write first word of sentence with capital letter”

print(sentence.capitalize())

Note the difference

print(sentence.title())
print(word.title())

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()