String comparison in Python; examples + exercises

Published: 28 July 2020
on channel: Olga Soboleva
155
1

#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程式設 #程式設計語言 #程式設


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