문자열 my_string과 정수 n이 매개변수로 주어질 때, my_string에 들어있는 각 문자를 n만큼 반복한 문자열을 return 하도록 solution 함수를 완성해보세요.
join()
- The join() method takes all items in an iterable and joins them into one string.
반복 가능한 문자열을 가져와 하나의 문자열로 결합합니다. - A string must be specified as the separator.
구분자를 지정해야 합니다. - string.join(iterable)
Parameter | Description |
iterable | Required. Any iterable object where all the returned values are strings |
code - 1
def solution(my_string, n):
answer = ''
for i in my_string:
answer += (i*n)
return answer
- for문 → answer += (i*n)
- my_string을 한 글자씩 i로 for문을 돌립니다.
- answer에 i*n(i를 n만큼 반복)을 더합니다.
- return answer
- 위 과정을 통해 더해진 answer 문자열을 return 합니다.
code - 2
def solution(my_string, n):
return ''.join(s*n for s in my_string)
- ''.join(s*n for s in my_string)
- my_string을 한 글자씩 s로 for문을 돌립니다.
- s*n 즉 s를 n만큼 반복한 값을 반환합니다.
- ''.join 위 값을 join 함수를 이용해 한 문자열로 만들어줍니다.
https://github.com/seonmin5/codingtest_Python
GitHub - seonmin5/codingtest_Python
Contribute to seonmin5/codingtest_Python development by creating an account on GitHub.
github.com
반응형
'Python' 카테고리의 다른 글
[Python/level 0] 대문자와 소문자 - 120893, swapcase() (0) | 2024.08.17 |
---|---|
[Python/level 0] 특정한 문자를 대문자로 바꾸기 - 181873, replace() (0) | 2024.08.17 |
[Python/level 0] 특정 문자 제거하기 - 120826, replace() (0) | 2024.08.17 |
[Python/level 0] 최댓값 만들기 (1) - 120847, sort(), sort(reverse=True) (0) | 2024.08.17 |
[Python/level 1] 자연수 뒤집어 배열로 만들기 - 12932, reversed() (1) | 2024.08.17 |