in Operator
in
operator helps in looping and counting within some string or list.
a = "Cloudxlab is a large ed-tech platform for ml and big data"
count = 0
for letter in a:
if letter == 'l':
count = count + 1
letter
is the iteration variable that iterates over the string from 1st character to the last using the for
loop and the in
operator. It checks if the letter
is equal to the character l
, it increases the counter.
Enter the value of count
when the loop is over
The in
operator can also work upon 2 strings and return True
or False
depending upon whether the 1st string is present as a substring in the 2nd string.
result = 'cloud' in 'cloudxlab'
print (result)
What will this print?
Apart from checking for a substring, we can also compare strings. The comparison operators work on strings,
string = "machine"
print (string == " machine")
What will it print?
Other comparison operators like <
, >
, <=
and >=
are useful for putting words in alphabetical order: Try comparing various strings in the notebook and observe the results.
All the uppercase letters come before all the lowercase letters in Python.
Loading comments...