String comparison in Python; examples + exercises

Pubblicato il: 28 luglio 2020
sul canale di: 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程式設 #程式設計語言 #程式設


In questa pagina del sito puoi guardare il video online String comparison in Python; examples + exercises della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Olga Soboleva 28 luglio 2020, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 155 volte e gli è piaciuto 1 spettatori. Buona visione!