본문 바로가기
프로그래밍/코딩 테스트

파이썬 코테용 유용한 함수 모음

by Devry 2025. 3. 23.

문자열 다루기

 

문자열을 대문자로 바꾸기 string.upper()

myString = 'hello world'
 print(myString.upper())
# HELLO WORLD

문자열을 소문자로 바꾸기 string.lower()

myString = 'HELLO WORLD'
 print(myString.lower())
# hello world

모든 문자열이 대문자인지 확인 string.isupper()

하나라도 소문자가 포함되면 False

upperString = 'HELLO WORLD'
print(upperString.isupper())
# True

lowerString = 'hello world'
print(lowerString.isupper())
# False

모든 문자열이 소문자인지 확인 string.islower()

하나라도 대문자가 포함되면 False

lowerString = 'hello world'
print(lowerString.islower())
# True

upperString = 'HELLO WORLD'
print(upperString.islower())
# False

 

문자열의 대소문자 변환 string.swapcae()

대문자 -> 소문자

소문자 -> 대문자

mixedString = 'hello WORLD'
print(mixedString.swapcase())
# HELLO world

 

두 수의 최대공약수 math.gcd(a,b)

import math
a = 10
b = 15
gcd = math.gcd(a, b)
print(gcd)
# 5

두 수의 최소공배수 a * b / math.gcd(a, b)

import math
a = 10
b = 15
lcm = a * b / math.gcd(a, b)
print(lcm)
# 30

댓글