이 문자열에서 "Johnson"앞뒤의 모든 소문자를 제거하려면 어떻게해야합니까?
str1 = 'aBcdJohnsonzZz'
str2 = 'asdVJohnsonkkk'
예상 결과는 다음과 같습니다.
str1 = 'BJohnsonZ'
str2 = 'VJohnson'
소문자를 번역하는 것보다 문자열을 분할하고 구분 기호가 있는지 확인할 수 있습니다. 예 :
from string import ascii_lowercase as alc
str1 = 'aBcdJohnsonzZz'
p1, sep, p2 = str1.partition('Johnson')
if sep:
str1 = p1.translate(None, alc) + sep + p2.translate(None, alc)
print str1
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다