Raza Zaidi
razacodes

razacodes

❇️ Python Tech Trivia ❇️ Chapter 2

❇️ Python Tech Trivia ❇️ Chapter 2

Raza Zaidi's photo
Raza Zaidi

Published on Sep 25, 2021

2 min read

Welcome to ❇️ Python Tech Trivia ❇️

These are some exercises I post on Twitter every day.

These exercises are meant for beginners to practice what you've learned.

After all, practice makes perfect!

Content

  • Exercises
  • Answers

Let's start :)

Exercise 1

word1 = 'now'
word2 = 'go'
word3 = 'and'
word4 = 'code'
word5 = 'some'
word6 = 'python'

print(f'{word1.title()} {word2} {word3.upper()} {word4} {word5} {word6.upper()} ')

Exercise 2

rython = 'Now go and code some Python'

print(rython.split())

Exercise 3

my_fav_language = 'Python rocks'

print(my_fav_language.rstrip(' rocks'))

Exercise 4

rython = 'Now go and code some PYTHON'

print(rython.lstrip('Now go and code some '))

Exercise 5

rython = 'Now go and code some python'

if 'python' in rython:
    print(rython.upper())

Answers

Did you do all the exercises first? You didn't cheat did you!?

Alright go ahead. Here are the answers.

Exercise 1

word1 = 'now'
word2 = 'go'
word3 = 'and'
word4 = 'code'
word5 = 'some'
word6 = 'python'

print(f'{word1.title()} {word2} {word3.upper()} {word4} {word5} {word6.upper()} ')
>>>Now go AND code some PYTHON

Exercise 2

rython = 'Now go and code some Python'

print(rython.split())
>>>['Now', 'go', 'and', 'code', 'some', 'Python']

Exercise 3

my_fav_language = 'Python rocks'

print(my_fav_language.rstrip(' rocks'))
>>> Python

There is something I want to clarify when using the 'strip', 'rstrip' or 'lstrip' method. You might think that the word 'rocks' is stripped, but this function strips the characters passed through the function. So the letters 'r', 'o', 'c', 'k', 's'.

Now you might say why wasn't the output 'Pythn', because we passed an 'o'. That is because the function stops as soon as it encounters a character that wasn't passed. In this case it works from right to left. Stripping each character in the process. Then encounters 'n' and stops, because we didn't pass the character 'n'.

I know there are better functions for this result, but just wanted to show how this functions works.

Exercise 4

rython = 'Now go and code some PYTHON'

print(rython.lstrip('Now go and code some '))
>>> PYTHON

Exercise 5

rython = 'Now go and code some python'

if 'python' in rython:
    print(rython.upper())
>>> NOW GO AND CODE SOME PYTHON
 
Share this