Загрузка...

Python String Methods part 2 #pythonforbeginners #python3 #phython #learnpython #pythontutorial

The `strip()` method in Python is a string method used to remove leading and trailing whitespaces (spaces, tabs, and newlines) from a string. The method returns a new string with the leading and trailing whitespaces removed.

Here's an example:

```python
text = " Python Programming "
stripped_text = text.strip()

print("Original:", repr(text)) # repr() is used to display whitespaces
print("Stripped:", repr(stripped_text))
```

Output:
```
Original: ' Python Programming '
Stripped: 'Python Programming'
```

In this example, the `strip()` method removes the leading and trailing whitespaces from the original string, resulting in a new string without those spaces.

It's important to note that `strip()` only removes leading and trailing whitespaces, not spaces within the string.

You can also use `lstrip()` and `rstrip()` if you want to remove whitespaces only from the left or right side of the string, respectively.

Here's an example:

```python
text = " Python Programming "
left_stripped_text = text.lstrip()
right_stripped_text = text.rstrip()

print("Original:", repr(text))
print("Left Stripped:", repr(left_stripped_text))
print("Right Stripped:", repr(right_stripped_text))
```

Output:
```
Original: ' Python Programming '
Left Stripped: 'Python Programming '
Right Stripped: ' Python Programming'
```

In this example, `lstrip()` removes leading whitespaces, and `rstrip()` removes trailing whitespaces.

Видео Python String Methods part 2 #pythonforbeginners #python3 #phython #learnpython #pythontutorial канала pythonbuzz
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять