Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Strings Have Length | Strings
Python Data Types

book
Strings Have Length

There is also a very useful function in Python that can give the length of a string -- len() function. Let's look at the example of using this function.

string = 'watermelon'
length = len(string)
print(f'the length of the string "{string}" is {length}')
123
string = 'watermelon' length = len(string) print(f'the length of the string "{string}" is {length}')
copy
Завдання

Swipe to start coding

You have such strings apple,grape, orange. You have to get the length of each of these strings. Using such comparison operators >, < determine the longest string.

Рішення

str_1 = 'apple'
str_2 = 'grape'
str_3 = 'orange'

len_1 = len(str_1)
len_2 = len(str_2)
len_3 = len(str_3)

print(f'str_1 is greater than str_2? {str_1 > str_2}')
print(f'The length of str_1 is {len_1}, the length of str_2 is {len_2}')
print(f'str_1 is greater than str_3? {str_1 > str_3}')
print(f'The length of str_1 is {len_1}, the length of str_3 is {len_3}')
print(f'str_2 is greater than str_3? {str_2 > str_3}')
print(f'The length of str_2 is {len_2}, the length of str_3 is {len_3}')

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 8
str_1 = 'apple'
str_2 = 'grape'
str_3 = 'orange'

len_1 = _ _ _
len_2 = _ _ _
len_3 = _ _ _

print(f'str_1 is greater than str_2? {str_1 _ _ _ str_2}')
print(_ _ _'The length of str_1 is {_ _ _}, the length of str_2 is {_ _ _}')
print(f'str_1 is greater than str_3? {_ _ _}')
print(_ _ _'The length of str_1 is {_ _ _}, the length of str_3 is {_ _ _}')
print(f'str_2 is greater than str_3? {_ _ _}')
print(_'The length of str_2 is {_ _ _}, the length of str_3 is {_ _ _}')
toggle bottom row
some-alt