2012-09-12 3 views
0

코드의 다음 부분을 고려 str.lstrip (문자)를 사용할 수없는 경우에는 자이 2.1 같은 달성하는 방법는 <2.1

word='hello.world' 
//matchedWord to contain everything right of "hello" 
matchedWord=word.lstrip('hello') //now matchedWord='.world' 

. 단어의 왼쪽에있는 모든 문자를 제거하는 다른 작업 주변? 당신이 정말로 .lstrip()를 사용해야하는 경우

+1

참고 단어에서'hello'를 제거하는 것은 당신이 문자'h','e','l'과를 제거하려는 의미'O' 주문에 관계없이 그 단어에서! 'if word.startswith ('hello') : word = word [len ('hello') :]'를 대신 찾고 있습니다. –

답변

0

당신이 함수로 그것을 구현할 수 있습니다

def lstrip(value, chars=None): 
    if chars is None: 
     chars=' \t\n' 
    while value and value[0] in chars: 
     value = value[1:] 
    return value 

하지만 .lstrip() (그리고 .rstrip().strip())는 를 제거 무엇을 알고 있어야으로부터 문자 설정 프론트가 아니라 프론트. (cpython) 문서에서 :

선행 문자가 제거 된 문자열의 사본을 반환하십시오. chars 인수는 제거 할 문자 집합을 지정하는 문자열입니다. 생략되거나 None 인 경우 chars 인수의 기본값은 공백 인 입니다. chars 인수는 접두사가 아닙니다. 오히려, 그 값의 모든 조합이 제거됩니다 :

>>> ' spacious '.lstrip() 
'spacious ' 
>>> 'www.example.com'.lstrip('cmowz.') 
'example.com'