Python

[파이썬] lower | upper

pyflu 2023. 9. 28. 12:27

 

파이썬에서 'lower()' 과 'upper()'은 문자열 메서드(method)로 문자열의 대소문자를 변환하는 역할이다.


1. lower()

- 'lower()'은 메서드는 문자열의 모든 알파벳 문자를 "소문자"로 변환합니다.

- 대문자 알파벳 문자는 소문자로 변환되며, 나머지 문자(숫자, 특수 문자 등)는 변화하지 않습니다.

 

text = "Hi123 #$%^ World!!!"
lower_text = text.lower()
print(lower_text)  # 출력: "hi123 #$%^ world!!!"

2. upper()

- 'upper()' 메서드는 문자열의 모든 알파벳 문자를 "대문자"로 변환한다.
- 소문자 알파벳 문자는 대문자로 변환되며, 나머지 문자(숫자, 특수 문자 등)는 변화하지 않는다.

text = "11Hello99  $$$###World***"
upper_text = text.upper()
print(upper_text)  # 출력: "11HELLO99 $$$###WORLD***"

★ 주의할 점

'lower()'와 'upper()' 메서드는 원본 문자열을 변경하지 않고 새로운 문자열을 반환한다는 것입니다.

문자열은 불변(immutable) 객체이기 때문에 메서드를 호출해도 원본 문자열이 변경되지 않습니다.

따라서 변환된 값을 변수에 할당하여 사용해야 합니다.

 

ㅡㅡㅡㅡㅡㅡㅡㅡ할당Xㅡㅡㅡㅡㅡㅡㅡㅡㅡ

text1 = "Hello World"
text1.lower()
print(text1)  # 출력: "Hello World"

text2 = "Hello World"
text2.upper()
print(text2)  # 출력: "Hello World"

ㅡㅡㅡㅡㅡㅡㅡㅡ할당Oㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

text3 = "Hello World"
lower_text3 = text3.lower()
print(lower_text3)  # 출력: "hello world"

text4 = "Hello World"
upper_text4= text4.upper()
print(upper_text4)  # 출력: "HELLO WORLD"
728x90

'Python' 카테고리의 다른 글

[Python] 파이썬 람다(lambda)함수 정리  (59) 2023.09.28
[Python] 파이썬 정렬 - sorted 함수 정리  (60) 2023.09.28
[파이썬] islower | isupper  (32) 2023.09.28
[파이썬] title | capitalize  (38) 2023.09.28
[파이썬] swapcase  (50) 2023.09.28