Загрузка страницы

String comparison in Python; examples + exercises

#python #stringspython #asciicode #stringcomparison

Exercises:

Compare the following strings in two ways - with the len() function and without using the len() function:

1.

x = 'triceratops'
y = 'spinosaurus'
2.

x = 'relativism'
y = 'availability'

3.

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

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

______________________________________________________________________________________

Summary of the video

First example:

x = 'cupboard'
y = 'lamp'

First of all compared these strings using the len() function and if sentence. But after that we compared them without using the len() function and got an opposite result. In order to explain it we need to know what is ASCII code.

Let's take a look at ASCII conversion table.

Second example:

m = 'chair'
n = 'table'

Although these two strings contain the same of characters, in the comparison without len() the string 'table' is greater than 'chair', because the ASCII code of the letter t is greater than the ASCII code from the letter c.

ASCII codes from uppercase letters are less than ASCII codes from lowercase letters.

How Python compares strings without the len() function?

It compares ASCII codes from each character going character for character. If the codes from the first character of each strings are different Python immediately gives the result of comparison and doesn't move to the next character.

Third example:

m = 'chair'
n = 'Table'

Fourth example:

m = 'chair'
z = 'cHair'

Fifth example:

g1 = 'How are you friend?'
g2 = 'How are you Friend?'

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

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

Видео String comparison in Python; examples + exercises канала Olga Soboleva
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
28 июля 2020 г. 20:18:37
00:18:18
Яндекс.Метрика