본문 바로가기

ProgrammingTips6

Python 문자열에서 퍼센트(%)를 선택적으로 이스케이프하는 방법은 무엇인가요?, How can I selectively escape percent (%) in Python strings? 질문 나는 다음과 같은 코드를 가지고 있습니다. test = "have it break." selectiveEscape = "Print percent % in sentence and not %s" % test print(selectiveEscape) 원하는 출력 결과는 다음과 같습니다: Print percent % in sentence and not have it break. 실제로 발생한 일은 다음과 같습니다: selectiveEscape = "Use percent % in sentence and not %s" % test TypeError: %d format: a number is required, not str 답변 >>> test = "have it break." >>> selectiveEscap.. 2023. 12. 5.
Python 파이썬으로 소수점 2자리로 반올림하는 방법은 무엇인가요? [중복], How to round to 2 decimals with Python? [duplicate] 질문 이 코드의 출력에서 많은 소수점을 얻고 있습니다 (화씨에서 섭씨로 변환). 현재 코드는 다음과 같습니다: def main(): printC(formeln(typeHere())) def typeHere(): global Fahrenheit try: Fahrenheit = int(raw_input("안녕하세요! 화씨 값을 입력하고 섭씨로 변환하세요!\n")) except ValueError: print "\n입력한 값이 숫자가 아닙니다!" print "화씨 값을 50으로 설정했습니다!" Fahrenheit = 50 return Fahrenheit def formeln(c): Celsius = (Fahrenheit - 32.00) * 5.00/9.00 return Celsius def printC(ans.. 2023. 9. 10.
Python 파이썬에서 16진수 문자열을 정수로 변환하기, Convert hex string to integer in Python 질문 16진수 문자열을 정수로 변환하는 방법은 무엇인가요? "0xffff" ⟶ 65535 "ffff" ⟶ 65535 답변 0x 접두사 없이는 기본을 명시해야 합니다. 그렇지 않으면 구분할 수 있는 방법이 없습니다: x = int("deadbeef", 16) 0x 접두사가 있으면 Python은 16진수와 10진수를 자동으로 구분할 수 있습니다: >>> print(int("0xdeadbeef", 0)) 3735928559 >>> print(int("10", 0)) 10 (이 접두사 추측 동작을 호출하려면 반드시 기본값으로 0을 지정해야 합니다. 두 번째 매개변수를 생략하면 int() 함수는 기본값으로 10진수를 가정합니다.) 2023. 6. 6.
Python 파이썬 프로그램 실행 시간을 어떻게 얻을 수 있나요?, How do I get time of a Python program's execution? 질문 나는 Python에서 명령 줄 프로그램이 있고 완료하는 데 시간이 걸린다. 실행이 완료되는 정확한 시간을 알고 싶다. timeit 모듈을 살펴보았지만, 작은 코드 조각에 대해서만 작동하는 것 같다. 전체 프로그램의 시간을 측정하고 싶다. 답변 파이썬에서 가장 간단한 방법은 다음과 같습니다: import time start_time = time.time() main() print("--- %s seconds ---" % (time.time() - start_time)) 이는 프로그램이 최소한 0.1초 이상 걸리는 것으로 가정합니다. 출력 결과: --- 0.764891862869 seconds --- 2023. 5. 19.