Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Check String | Strings
Python Data Types

book
Check String

Very often it hits that we need to check for the presence of some character or word in the string, and in order to realize this, we should use such a key word as in. As a result, we get a boolean value: True if such a character or substring is present in the string, and False - in the opposite case. For a better understanding, let's look at an example.

string = 'Pear Lime Banana'
print('Lime' in string)
print('Mango' in string)
123
string = 'Pear Lime Banana' print('Lime' in string) print('Mango' in string)
copy
Tarefa

Swipe to start coding

You have a string. You should check that this line contains such substrings Lorem, python, consectetur. If there is such a substring, then calculate its length.

Solução

string = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''

sub_str1 = 'Lorem'
sub_str2 = 'python'
sub_str3 = 'consectetur'
if sub_str1 in string:
print(f'The length of {sub_str1} is {len(sub_str1)}')
else:
print(f'This line does not contain {sub_str1}')
if sub_str2 in string:
print(f'The length of {sub_str2} is {len(sub_str2)}')
else:
print(f'This line does not contain such word as `{sub_str2}`')
if sub_str3 in string:
print(f'The length of {sub_str3} is {len(sub_str3)}')
else:
print(f'This line does not contain {sub_str3}')

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 9
string = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''

sub_str1 = 'Lorem'
sub_str2 = '_ _ _'
sub_str3 = 'consectetur'

if sub_str1 _ _ _ string:
print(_ _ _'The length of {sub_str1} is {_ _ _(sub_str1)}')
_ _ _:
print(_ _ _'This line does not contain {sub_str1}')
_ _ _ sub_str2 __ string:
print(_ _ _'The length of {sub_str2} is {_ _ _(sub_str2)}')
else:
print(_ _ _'This line does not contain such word as `{sub_str2}`')
if _ _ _ in _ _ _:
print(_ _ _'The length of {sub_str3} is {_ _ _(sub_str3)}')
else:
print(_ _ _'This line does not contain {sub_str3}')

toggle bottom row
some-alt