파이썬 문자열 | 조각()
파이썬 끈 조각() 전달된 문자열 인수에 따라 선행 및 후행 문자가 모두 제거된 문자열의 복사본을 반환하는 Python 프로그래밍 언어의 내장 함수입니다. 이 기사에서는 Python 프로그램에서 이를 성공적으로 사용하는 방법을 철저하게 이해할 수 있도록 Strip() 메서드의 다양한 기능과 사용 사례를 검토합니다.
Python Strip() 메서드 구문
통사론: 문자열.스트립([문자])
매개변수: 여기에는 선택적 매개변수가 하나만 있습니다. 문자 – 제거할 문자 집합을 지정하는 문자열입니다. 선택적 chars 매개변수가 제공되지 않으면 모든 선행 및 후행 공백이 문자열에서 제거됩니다.
반환 값: 선행 및 후행 문자가 모두 제거된 문자열의 복사본을 반환합니다.
Python Strip() 함수의 목적
개발자가 문자열의 시작이나 끝에서 문자나 공백을 제거하려는 경우 다음의 Strip() 함수를 사용하세요. 파이썬 유용합니다. 좀 더 자세히 살펴보겠습니다:
- Strip() 함수는 Strip() 함수()에 인수로 제공된 문자에 대해 문자열의 시작이나 끝에서 문자를 제거하는 데 도움을 줍니다.
- 문자열에 공백이 없고 문자 인수가 제공되지 않은 경우 문자열은 있는 그대로 반환됩니다.
- 텍스트의 시작과 끝에서 공백을 제거하는 것도 좋습니다.
- 문자열에 공백이 포함되어 있고 문자 인수가 제공되지 않은 경우 공백을 구분한 후 문자열이 반환됩니다.
Python 예제의 문자열 스트립()
~ 안에 파이썬 , strip()> 선행 및 후행을 제거하는 데 메서드가 사용됩니다. 공백 문자 (공백, 탭 및 줄 바꿈)을 문자열에서 사용합니다. 공백 문자가 제거된 새 문자열을 반환합니다. 원래 문자열은 변경되지 않고 그대로 유지됩니다.
예
파이썬3
my_string> => ' Hello, world! '> stripped_string> => my_string.strip()> > print> (stripped_string)> |
산출
Hello, world!
Strip() 함수를 사용한 Python 스트리핑 문자열
이 예에서는 Python을 사용하겠습니다. 문자열 다듬기 그리고 우리는 문자열을 사용했고, 스트립() 함수를 끈 그리고 끈 없이.
파이썬3
string> => ''' geeks for geeks '''> > # prints the string without stripping> print> (string)> > # prints the string by removing leading and trailing whitespaces> print> (string.strip())> > # prints the string by removing geeks> print> (string.strip(> ' geeks'> ))> |
산출
geeks for geeks geeks for geeks for
Strip() 함수를 사용하여 Python 스트리핑 특정 문자
이 예에서는 파이썬 문자열 Trim 및 우리는 string() 함수를 사용하여 문자열에서 특정 문자 집합을 제거했습니다.
파이썬3
# Python Program to demonstrate use of strip() method> > str1> => 'geeks for geeks'> # Print the string without stripping.> print> (str1)> > # String whose set of characters are to be> # remove from original string at both its ends.> str2> => 'ekgs'> > # Print string after stripping str2 from str1 at both its end.> print> (str1.strip(str2))> |
산출
geeks for geeks for
Python Strip() 함수를 사용하여 공백 제거
이 예에서는 파이썬 문자열 Trim 그리고 우리는 string() 함수를 사용하여 문자열의 양쪽 끝에서 공백을 제거했습니다.
파이썬3
# Python Program to demonstrate use of strip() method without any argument> str1> => ''' geeks for geeks '''> > # Print the string without stripping.> print> (str1)> > # Print string after removing all leading> # and trailing whitespaces.> print> (str1.strip())> |
입력
geeks for geeks
산출
geeks for geeks
Python Strip() 함수를 사용하여 NewLine 제거
이 예에서는 Python String Trim을 사용하고 Strip() 함수를 사용하여 문자열을 제거합니다. 개행 문자 문자열에서.
파이썬3
string> => '
Hello, World!
'> new_string> => string.strip()> print> (new_string)> |
산출
Hello, World!
실용적인 응용 프로그램
문자열이 주어지면 처음과 끝에서 단어 the의 발생을 제거합니다. 우리는 Python String Trim을 할 것입니다.
파이썬
# Python3 program to demonstrate the practical application> # strip()> > string> => ' the King has the largest army in the entire world the'> > # strip function works on characters and removes characters till it sees,> # the last or beginning characters mentioned in the function has been removed> print> (string.strip(> ' eht'> ))> |
입력
the King has the largest army in the entire world the
산출
King has the largest army in the entire world