Prepare > Python > Strings > Capitalize!
2023. 7. 15. 14:28ㆍHackerRank-Python
Capitalize! | HackerRank
Capitalize Each Word.
www.hackerrank.com
문제
You are asked to ensure that the first and last names of people begin with a capital letter in their passports. For example, alison heck should be capitalised correctly as Alison Heck.
코드
def solve(s):
return ' '.join(map(str.capitalize, s.split(' ')))
노트
map은 리스트의 요소를 지정된 함수로 처리해주는 함수(원본 리스트 보존, 새 리스트 리턴)
# 반복문 이용
a = [1.2, 2.5, 3.7, 4.6]
for i in range(len(a)):
a[i] = int(a[i])
print(a) # [1, 2, 3, 4]
# map함수 이용
a = [1.2, 2.5, 3.7, 4.6]
a = list(map(int, a))
print(a) # [1, 2, 3, 4]
capitalize() 함수: 맨 첫 문자만 대문자로 변환
def solve(s):
a = s.split(' ') # ["hello", "world"]
b =[] # 빈 리스트 생성
for i in a: # 리스트에 대해 반복
b.append(str(i).capitalize()) # ['Hello', 'World'] hello -> Hello 로 만들고, 리스트 b에 추가.
return ' '.join(b) # 리스트를 공백(구분자)으로 이어붙임
- capitalize()는 문자열만 인식
- upper(): 모든 알파벳을 대문자로
- title(): 알파벳 이외의 문자(공백, 숫자, 특수문자 등)로 나누어져있는 모든 영어의 첫 문자를 대문자로 변환
더보기
INPUT
a = "abC de3f_gh"
print(a.upper())
print(a.capitalize())
print(a.title())
OUTPUT
ABC DE3F_GH
Abc de3f_gh
Abc De3F_Gh
참조
파이썬 코딩 도장: 22.6 리스트에 map 사용하기
이번에는 리스트에 map을 사용해보겠습니다. map은 리스트의 요소를 지정된 함수로 처리해주는 함수입니다(map은 원본 리스트를 변경하지 않고 새 리스트를 생성합니다). list(map(함수, 리스트)) tupl
dojang.io
[python] 대문자로 변환하기 upper() / capitalize() / title()
해커랭크 이 문제를 풀다가..대문자로 변환하는 upper()만 알고 있어서 조금 헤맸는데 알고보니까 capitalize() 를 알고 있으면 완전 쉬운 문제였다..! 그래서 이번엔 대문자로 변환하는 함수들을 정
pearlluck.tistory.com
'HackerRank-Python' 카테고리의 다른 글
Prepare > Python > Sets > Symmetric Difference (0) | 2023.07.19 |
---|---|
Prepare > Python > Sets > Introduction to Sets (1) | 2023.07.17 |
Prepare > Python > Strings > Alphabet Rangoli (0) | 2023.07.14 |
Prepare > Python > Strings > String Formatting (0) | 2023.07.12 |
Prepare > Python > Strings > Designer Door Mat (0) | 2023.07.11 |