Как применять функцию replace() к строкам в Python? Метод replace() в Python.

preview_player
Показать описание
#pythonпорусски #функцияreplace #методreplace #строкивpython

Упражнения:

1. Замените maturing sun на waning moon в данной строке:

p = 'Season of mists and mellow fruitfulness, close bosom-friend of the maturing sun'

2. Замените все буквы n буквой W в данной строке:

p = 'Season of mists and mellow fruitfulness, close bosom-friend of the maturing sun'

3. Замените king's на queen's в данной строке:

hd = 'All the king's horses and all the king's men couldn't put Humpty together again'

3. Замените все пробелы восклицательными знаками в данной строке:

hd = 'All the king's horses and all the king's men couldn't put Humpty together again'

______________________________________________________________________________________

Метод replace() это функция, которая ищет и заменяет любой символ или фрагмент строки.

Пример:

s = 'Beleet parus odinokiy'

Заменяем:

Beleet на Krasneet
Beleet parus на On
odinokiy на v tumane morya
e на X
Все пробелы на @

Курс №2 по программированию на Python 7
Python course in Russian
Курс по программированию на Питоне
Рекомендации по теме
Комментарии
Автор

В перовм блоке, допущена синтаксическая ошибка. К перменной s, автор присваивает переменную x. После этого он вызывает функцию: print(s). В таком случае, питон не выведит никакого результата. Правильней было написать: print(x)
Спасибо за ваш урок, он был для меня полезным 👍

alexandralexandr
Автор

# 1. Замените maturing sun на waning moon в данной строке:
p = 'Season of mists and mellow fruitfulness, close bosom-friend of the maturing sun'
x = p.replace('maturing sun', 'waning moon')
print(x)
# 2. Замените все буквы n буквой W в данной строке:
s = 'Season of mists and mellow fruitfulness, close bosom-friend of the maturing sun'
y = s.replace('n', 'W')
print(y)
# 3. Замените king's на queen's в данной строке:
hd = "All the king's horses and all the king's men couldn't put Humpty together again"
a = hd.replace("king's", "queen's")
print(a)
# 4. Замените все пробелы восклицательными знаками в данной строке:
hb = "All the king's horses and all the king's men couldn't put Humpty together again"
b = hb.replace(' ', '!')
print(b)
print('Thank you Olya for the lessons and for the exercises')

Max-ih