Python에서 여러 인수 인쇄
이건 내 코드의 일부분일 뿐이야
print("Total score for %s is %s ", name, score)
하지만 난 그걸 출력해 내길 원해:
" (이름)의 총점은 (점수)"
어디에name
리스트에 변수가 있고score
정수다.이게 도움이 된다면 Python 3.3 입니다.
이렇게 하는 데는 여러 가지 방법이 있다.다음을 사용하여 현재 코드를 수정하려면%
-1987년, 투플레로 통과해야 한다.
튜플로 전달:
print("Total score for %s is %s" % (name, score))
하나의 요소가 있는 튜플은 다음과 같이 보인다.('this',)
.
이를 수행하는 다른 일반적인 방법은 다음과 같다.
사전으로 전달:
print("Total score for %(n)s is %(s)s" % {'n': name, 's': score})
또한 새로운 스타일의 문자열 형식도 있는데, 이 형식은 다음과 같이 읽기가 조금 더 쉬울 수 있다.
새 형식의 문자열 형식 사용:
print("Total score for {} is {}".format(name, score))
숫자와 함께 새로운 형식의 문자열 형식 사용(동일한 문자열을 여러 번 다시 정렬하거나 인쇄하는 데 유용함):
print("Total score for {0} is {1}".format(name, score))
명시적 이름에 새로운 형식의 문자열 형식 사용:
print("Total score for {n} is {s}".format(n=name, s=score))
문자열 연결:
print("Total score for " + str(name) + " is " + str(score))
내 생각에 가장 분명한 두 사람은
값을 매개 변수로 전달하십시오.
print("Total score for", name, "is", score)
다음에 의해 자동으로 공간이 삽입되지 않도록 하려면
print
위의 예에서 변경하십시오.sep
매개 변수:print("Total score for ", name, " is ", score, sep='')
파이톤 2를 사용하고 있다면 마지막 두 개는 사용하지 못할 겁니다.
print
파이톤 2에서는 기능이 없어그러나 다음에서 이 동작을 가져올 수 있다.__future__
:from __future__ import print_function
신품 사용
f
-Python 3.6의 문자열 형식:print(f'Total score for {name} is {score}')
그것을 인쇄하는 데는 여러 가지 방법이 있다.
다른 예를 들어 봅시다.
a = 10
b = 20
c = a + b
#Normal string concatenation
print("sum of", a , "and" , b , "is" , c)
#convert variable into str
print("sum of " + str(a) + " and " + str(b) + " is " + str(c))
# if you want to print in tuple way
print("Sum of %s and %s is %s: " %(a,b,c))
#New style string formatting
print("sum of {} and {} is {}".format(a,b,c))
#in case you want to use repr()
print("sum of " + repr(a) + " and " + repr(b) + " is " + repr(c))
EDIT :
#New f-string formatting from Python 3.6:
print(f'Sum of {a} and {b} is {c}')
사용:.format()
:
print("Total score for {0} is {1}".format(name, score))
또는:
// Recommended, more readable code
print("Total score for {n} is {s}".format(n=name, s=score))
또는:
print("Total score for" + name + " is " + score)
또는:
print("Total score for %s is %d" % (name, score))
또는:f-string
Python 3.6에서 포맷:
print(f'Total score for {name} is {score}')
사용할 수 있음repr
그리고 자동적으로''
추가됨:
print("Total score for" + repr(name) + " is " + repr(score))
# or for advanced:
print(f'Total score for {name!r} is {score!r}')
파이톤 3.6에서는f-string
훨씬 깨끗하다.
이전 버전에서는:
print("Total score for %s is %s. " % (name, score))
Python 3.6의 경우:
print(f'Total score for {name} is {score}.')
할 거다.
그것은 더 효율적이고 우아하다.
간단히 말하자면, 나는 개인적으로 끈 결합을 좋아한다.
print("Total score for " + name + " is " + score)
그것은 Python 2.7 a 3.X와 함께 작동한다.
참고: 점수가 int인 경우 str:
print("Total score for " + name + " is " + str(score))
그냥 이것만 따라와.
grade = "the biggest idiot"
year = 22
print("I have been {} for {} years.".format(grade, year))
OR
grade = "the biggest idiot"
year = 22
print("I have been %s for %s years." % (grade, year))
그리고 다른 모든 것을 잊어버려라, 그렇지 않으면 뇌가 모든 형식을 지도화하지 못할 것이다.
다음을 시도해 보십시오.
print("Total score for", name, "is", score)
사용하다f-string
:
print(f'Total score for {name} is {score}')
아니면
사용하다.format
:
print("Total score for {} is {}".format(name, score))
print("Total score for %s is %s " % (name, score))
%s
으로 대체될 수 있다.%d
또는%f
만약score
그렇다면 숫자다.
print("Total score for %s is %d" % (name, score))
점수가 문자열인 경우
print("Total score for %s is %s" % (name, score))
점수가 숫자라면, 그 다음이다.%d
, 그것이 끈이라면, 그 다음이다.%s
, 점수가 부동이라면, 그 다음이다.%f
내가 하는 일은 다음과 같다.
print("Total score for " + name + " is " + score)
뒤에 공백을 넣는 것을 기억하라.for
전후로is
.
이것은 아마도casting issue
.Casting syntax
두 가지 다른 것을 결합하려고 할 때 발생한다.types of variables
. 변환할 수 없기 때문에string
완전히integer
또는float
항상, 우리는 우리의 것을 전환해야 한다.integers
.string
이렇게 하는 거야 str(x)
. 정수로 변환하려면:int(x)
, 그리고 부유물은float(x)
당사의 코드는 다음과 같다.
print('Total score for ' + str(name) + ' is ' + str(score))
또한! 이것 좀 실행해봐.snippet
을 표로 types of variables
!
<table style="border-collapse: collapse; width: 100%;background-color:maroon; color: #00b2b2;">
<tbody>
<tr>
<td style="width: 50%;font-family: serif; padding: 3px;">Booleans</td>
<td style="width: 50%;font-family: serif; padding: 3px;"><code>bool()</code></td>
</tr>
<tr>
<td style="width: 50%;font-family: serif;padding: 3px">Dictionaries</td>
<td style="width: 50%;font-family: serif;padding: 3px"><code>dict()</code></td>
</tr>
<tr>
<td style="width: 50%;font-family: serif;padding: 3px">Floats</td>
<td style="width: 50%;font-family: serif;padding: 3px"><code>float()</code></td>
</tr>
<tr>
<td style="width: 50%;font-family: serif;padding:3px">Integers</td>
<td style="width: 50%;font-family: serif;padding:3px;"><code>int()</code></td>
</tr>
<tr>
<td style="width: 50%;font-family: serif;padding: 3px">Lists</td>
<td style="width: 50%font-family: serif;padding: 3px;"><code>list()</code></td>
</tr>
</tbody>
</table>
가장 쉬운 방법은 다음과 같다.
print(f"Total score for {name} is {score}")
그냥 앞에 "f"를 놓아라.
참조URL: https://stackoverflow.com/questions/15286401/print-multiple-arguments-in-python
'IT이야기' 카테고리의 다른 글
사용 가능한 업데이트 컨텐츠 데이터 문제 (0) | 2022.03.13 |
---|---|
Vuex, Vue-라우터 및 처리 페이지 다시 로드(F5) (0) | 2022.03.12 |
'인쇄' 출력을 파일로 리디렉션하는 방법? (0) | 2022.03.12 |
ResactRedx 툴킷 상태에서 어레이가 정의되지 않은 이유 (0) | 2022.03.12 |
컨텍스트가 Redex connect()를 통해 전달되지 않음 (0) | 2022.03.12 |