Looping through strings in Python; examples + exercises

Published: 20 July 2020
on channel: Olga Soboleva
175
8

#python #stringspython #forloops #loopingthroughstrings

Exercises:

1. How many characters are there in these strings?

w = 'availability'

hd = 'All the king's horses and all the king's men couldn't put Humpty together again!'

2. How many letters

a
i
l

are there in the following string?

w = 'availability'

3. How many words king , apostrophes, letters a are there in the following string?

hd = 'All the king's horses and all the king's men couldn't put Humpty together again!'

Use a loop for to answer.

______________________________________________________________________________________

We already know how to loop through lists in Python, let's see how to loop through strings in Python. The algorithm of this looping is exactly the same.

String example:

f = 'kiwi'

We counted number of letters in this string, number of letters 'i' and demonstrated the index number of each letter of this string in Python.

Also we know when to replace the iteration variable by the underscore and how to apply len function to strings and to lists.

We created a new variable and named it LETTER_TO_CHECK in order to avoid a difficult situation with the letter 'i'.

Python course #2 in English 10
Python programming language course in English

#underscore #pythonexercises #pythontasks #python程式設計語言 #python程式設 #程式設計語言 #程式設


On this page of the site you can watch the video online Looping through strings in Python; examples + exercises with a duration of hours minute second in good quality, which was uploaded by the user Olga Soboleva 20 July 2020, share the link with friends and acquaintances, this video has already been watched 175 times on youtube and it was liked by 8 viewers. Enjoy your viewing!