인용 부호 안에 인용 부호 사용
print파이썬 에서 명령 을 수행하고 싶을 때 따옴표를 사용해야 할 때 문자열을 닫지 않고 수행하는 방법을 모릅니다.
예를 들면 :
print " "a word that needs quotation marks" "
그러나 위에서 한 일을하려고하면 문자열이 닫히고 따옴표 사이에 필요한 단어를 넣을 수 없습니다.
어떻게 할 수 있습니까?
다음 세 가지 방법 중 하나로이 작업을 수행 할 수 있습니다.
1) 작은 따옴표와 큰 따옴표를 함께 사용하십시오.
>>> print '"A word that needs quotation marks"'
"A word that needs quotation marks"
2) 문자열 내에서 큰 따옴표를 이스케이프합니다.
>>> print "\"A word that needs quotation marks\""
"A word that needs quotation marks"
3) 삼중 따옴표로 묶인 문자열 사용 :
>>> print """ "A word that needs quotation marks" """
"A word that needs quotation marks"
이스케이프해야합니다.
>>> print "The boy said \"Hello!\" to the girl"
The boy said "Hello!" to the girl
>>> print 'Her name\'s Jenny.'
Her name's Jenny.
문자열 리터럴 은 파이썬 페이지를 참조하십시오 .
Python은 "와 '를 모두 따옴표로 허용하므로 다음과 같이 할 수 있습니다.
>>> print '"A word that needs quotation marks"'
"A word that needs quotation marks"
또는 내부 "s
>>> print "\"A word that needs quotation marks\""
"A word that needs quotation marks"
리터럴 이스케이프 문자 사용 \
print("Here is, \"a quote\"")
문자는 기본적으로 내 다음 문자 의 의미 론적 컨텍스트를 무시하고 문자 그대로 처리하는 것을 의미 합니다.
Windows의 Python 3.2.2에서
print(""""A word that needs quotation marks" """)
괜찮습니다. 파이썬 인터프리터의 향상이라고 생각합니다.
중복되는 한 가지 경우는 외부 프로세스에 따옴표를 사용해야한다는 것입니다. 이에 대한 해결 방법은 셸을 사용하지 않는 것입니다. 이렇게하면 한 수준의 인용에 대한 요구 사항이 제거됩니다.
os.system("""awk '/foo/ { print "bar" }' %""" % filename)
유용하게 대체 될 수 있습니다
subprocess.call(['awk', '/foo/ { print "bar" }', filename])
(또한 쉘 메타 문자가 쉘에서 filename이스케이프되어야 하는 버그를 수정 했습니다. 원래 코드는 실패했지만 쉘이 없으면 필요하지 않습니다).
물론 대부분의 경우 외부 프로세스를 원하거나 필요로하지 않습니다.
with open(filename) as fh:
for line in fh:
if 'foo' in line:
print("bar")
When you have several words like this which you want to concatenate in a string, I recommend using format or f-strings which increase readability dramatically (in my opinion).
To give an example:
s = "a word that needs quotation marks"
s2 = "another word"
Now you can do
print('"{}" and "{}"'.format(s, s2))
which will print
"a word that needs quotation marks" and "another word"
As of Python 3.6 you can use:
print(f'"{s}" and "{s2}"')
yielding the same output.
You could also try string addition: print " "+'"'+'a word that needs quotation marks'+'"'
ReferenceURL : https://stackoverflow.com/questions/9050355/using-quotation-marks-inside-quotation-marks
'Program Club' 카테고리의 다른 글
| 작업을 취소하면 예외가 발생합니다. (0) | 2021.01.09 |
|---|---|
| CSHTML 페이지에서 switch 문 구현 (0) | 2021.01.09 |
| Salesforce 인증 실패 (0) | 2021.01.09 |
| ImageButton에 Ontouch와 Onclick을 모두 사용하는 방법은 무엇입니까? (0) | 2021.01.09 |
| 부트 스트랩의 폼 컨트롤 팝 오버에서 필수 필드의 기본 메시지를 변경하는 방법은 무엇입니까? (0) | 2021.01.09 |