PYTHON_REAL Telegram 985
🧼 Методы strip(), lstrip() и rstrip() в Python: удаление пробелов и символов

Работая с текстом в Python, часто нужно удалить лишние пробелы или символы с краёв строки. Именно для этого существуют методы strip(), lstrip() и rstrip().

Основы


text = " hello world "
print(text.strip()) # 'hello world'
print(text.lstrip()) # 'hello world '
print(text.rstrip()) # ' hello world'


По умолчанию эти методы удаляют пробелы, переводы строк, табуляции и другие пробельные символы.

Удаление определённых символов

Можно передать в метод строку с символами, которые нужно удалить:


text = ".,!?hello world!?.,"
print(text.strip(".,!?")) # 'hello world'


Важно: метод удаляет все указанные символы, а не подстроку. То есть strip("abc") удалит все a, b и c, а не строку "abc".

Применение в реальной жизни

- Очистка данных перед обработкой.
- Удаление лишних символов после парсинга.
- Нормализация пользовательского ввода.

Совет

Если нужно удалить только начало или только конец строки — используй lstrip() или rstrip() соответственно.

https://realpython.com/python-strip/

#python

👉 @python_real
👍6



tgoop.com/python_real/985
Create:
Last Update:

🧼 Методы strip(), lstrip() и rstrip() в Python: удаление пробелов и символов

Работая с текстом в Python, часто нужно удалить лишние пробелы или символы с краёв строки. Именно для этого существуют методы strip(), lstrip() и rstrip().

Основы


text = " hello world "
print(text.strip()) # 'hello world'
print(text.lstrip()) # 'hello world '
print(text.rstrip()) # ' hello world'


По умолчанию эти методы удаляют пробелы, переводы строк, табуляции и другие пробельные символы.

Удаление определённых символов

Можно передать в метод строку с символами, которые нужно удалить:


text = ".,!?hello world!?.,"
print(text.strip(".,!?")) # 'hello world'


Важно: метод удаляет все указанные символы, а не подстроку. То есть strip("abc") удалит все a, b и c, а не строку "abc".

Применение в реальной жизни

- Очистка данных перед обработкой.
- Удаление лишних символов после парсинга.
- Нормализация пользовательского ввода.

Совет

Если нужно удалить только начало или только конец строки — используй lstrip() или rstrip() соответственно.

https://realpython.com/python-strip/

#python

👉 @python_real

BY Реальный Python




Share with your friend now:
tgoop.com/python_real/985

View MORE
Open in Telegram


Telegram News

Date: |

Hui said the messages, which included urging the disruption of airport operations, were attempts to incite followers to make use of poisonous, corrosive or flammable substances to vandalize police vehicles, and also called on others to make weapons to harm police. The main design elements of your Telegram channel include a name, bio (brief description), and avatar. Your bio should be: It’s yet another bloodbath on Satoshi Street. As of press time, Bitcoin (BTC) and the broader cryptocurrency market have corrected another 10 percent amid a massive sell-off. Ethereum (EHT) is down a staggering 15 percent moving close to $1,000, down more than 42 percent on the weekly chart. The group’s featured image is of a Pepe frog yelling, often referred to as the “REEEEEEE” meme. Pepe the Frog was created back in 2005 by Matt Furie and has since become an internet symbol for meme culture and “degen” culture. Members can post their voice notes of themselves screaming. Interestingly, the group doesn’t allow to post anything else which might lead to an instant ban. As of now, there are more than 330 members in the group.
from us


Telegram Реальный Python
FROM American