Conteúdo do Curso
Tipos de Dados em Python
Tipos de Dados em Python
Compare Strings
This chapter encompasses two distinct subjects: strings and boolean statements. The syntax we will cover here can prove quite valuable and significantly reduce the complexity of your code.
As I previously mentioned, operators such as >
, <
, ==
, >=
, and <=
can be quite efficient. Now, let's explore how they can be applied to strings.
Let's start with the simplest one: ==
. This compares two statements and returns True if they are equal the same way with strings:
print("Kate" == "Katty") print("Katty" == "Katty")
In the first case, the names are different, but in the second they are completely equal. It's not a piece of brand-new information; therefore, let's move on to the other signs. As you may recall, X > Y
means that X is greater than Y. But what can we say about the following operation?
print('A' < 'B')
The expression means True
because B is greater than A, but how can string be greater?
Note
There is a singular method for comparing letters according to their alphabetical order. This implies that within the alphabet, the letter
B
is considered greater thanA
as it is positioned further along. To organize items alphabetically or to perform personal assessments, you can utilize the>
and<
symbols.
By the way, we can compare even words. The algorithm of comparing is the following: It compares words by the first letters that differ:
print("alphabet" > "although")
Explanation: The first two letters are equal, so comparing starts from the third letter in each word (the first unequivalent letter).
Swipe to begin your solution
Practice makes perfect! 🤓
Your task is to compare the following words using either the >
or <
operator and assign each comparison result to a variable.
-
Compare
"C"
and"D"
and assign the result tostatement1
-
Compare
"Codefinity"
and"Art"
and assign the result tostatement2
-
Compare
"Programming"
and"Data"
and assign the result tostatement3
Solução
Obrigado pelo seu feedback!
Compare Strings
This chapter encompasses two distinct subjects: strings and boolean statements. The syntax we will cover here can prove quite valuable and significantly reduce the complexity of your code.
As I previously mentioned, operators such as >
, <
, ==
, >=
, and <=
can be quite efficient. Now, let's explore how they can be applied to strings.
Let's start with the simplest one: ==
. This compares two statements and returns True if they are equal the same way with strings:
print("Kate" == "Katty") print("Katty" == "Katty")
In the first case, the names are different, but in the second they are completely equal. It's not a piece of brand-new information; therefore, let's move on to the other signs. As you may recall, X > Y
means that X is greater than Y. But what can we say about the following operation?
print('A' < 'B')
The expression means True
because B is greater than A, but how can string be greater?
Note
There is a singular method for comparing letters according to their alphabetical order. This implies that within the alphabet, the letter
B
is considered greater thanA
as it is positioned further along. To organize items alphabetically or to perform personal assessments, you can utilize the>
and<
symbols.
By the way, we can compare even words. The algorithm of comparing is the following: It compares words by the first letters that differ:
print("alphabet" > "although")
Explanation: The first two letters are equal, so comparing starts from the third letter in each word (the first unequivalent letter).
Swipe to begin your solution
Practice makes perfect! 🤓
Your task is to compare the following words using either the >
or <
operator and assign each comparison result to a variable.
-
Compare
"C"
and"D"
and assign the result tostatement1
-
Compare
"Codefinity"
and"Art"
and assign the result tostatement2
-
Compare
"Programming"
and"Data"
and assign the result tostatement3
Solução
Obrigado pelo seu feedback!