[예시]
>>> spam = 'SpamSpamBaconSpamEggsSpamSpam'
>>> spam.strip('ampS')
'BaconSpamEggs'
*Source: Automate the Boring Stuff with Python
[Breakdown by Perplexity]
Let me break this down for you step by step so you can understand the code and the result.
Code Explanation
- The strip() Method:
- The Input String:
spam = 'SpamSpamBaconSpamEggsSpamSpam'
This is a string containing multiple repetitions of "Spam", with "Bacon" and "Eggs" in between.
- The Argument to strip():
spam.strip('ampS')
Step-by-Step Execution
- The original string is:
'SpamSpamBaconSpamEggsSpamSpam'
- Starting from the beginning:
- Now we reach "BaconSpamEggsSpamSpam":
- Moving to the end of the string:
- After stripping both ends, we are left with:
'BaconSpamEggs'
Final Result
The result of spam.strip('ampS') is:
'BaconSpamEggs'
Key Takeaways
- strip() only removes characters from the start and end of a string, not from the middle.
- The argument to strip() ('ampS') specifies individual characters to remove, not a specific sequence.
- Characters are stripped until a character not in the specified set ('ampS') is encountered at either end.
'Journey to CS > 프로그래밍 언어' 카테고리의 다른 글
| [파이썬] 정규 표현식(Regex) 2. 그룹화(Grouping) (예제: 전화번호 찾기) (0) | 2025.02.20 |
|---|---|
| [파이썬] 정규 표현식(Regex) 1. 컴파일, Match 객체 (예제: 전화번호 찾기) (0) | 2025.02.20 |
| [파이썬] pass 문을 자리표시자(placeholder)로 사용하기 (0) | 2025.02.16 |
| [파이썬] 함수 - 기본 매개변수 값 (Default Parameter Value) (0) | 2025.02.16 |
| [AI Journey] Intro. 내가 파이썬 공부를 시작한 이유... (0) | 2025.02.04 |