문자열 활용하기

개발자를 위한 Python 입문

Jasmin Ludolf

Senior Data Science Content Developer

문자열은 어디에나 있습니다!

문자열로 작성된 Hello

  • 메시지 표시하기
  • 텍스트 입력 처리하기
  • 파일 이름
  • 출력 형식 지정하기
  • 데이터 처리하기
1 ChatGPT로 생성된 이미지
개발자를 위한 Python 입문

Python은 작은따옴표와 큰따옴표 인식

# This works
ingredient_name = 'San Marzano tomatoes'

# This also works
ingredient_name = "San Marzano tomatoes"
  • ' = 아포스트로피 = 작은따옴표
개발자를 위한 Python 입문

큰따옴표의 장점

# Single quote string variable containing an apostrophe
ingredient_name = 'Chef's special seasoning'
print(ingredient_name)
SyntaxError: invalid syntax.
# Double quote string variable containing an apostrophe
ingredient_name = "Chef's special seasoning"
print(ingredient_name)
Chef's special seasoning
개발자를 위한 Python 입문

문장 및 단락

recipe_step = "Heat olive oil in a large pan and sauté garlic until fragrant"
개발자를 위한 Python 입문

여러 줄 문자열

# Create a string variable over multiple lines
recipe_instructions = """1. Bring a large pot of salted water to boil and cook pasta
2. Heat olive oil in a pan and sauté minced garlic until fragrant
3. Add chopped tomatoes and simmer for 10 minutes
4. Toss cooked pasta with tomato sauce and fresh basil leaves
"""
  • """text""": 여러 줄 문자열
    • 가독성 향상
    • 문서화에 사용됨
    • 긴 텍스트(예: 지침 또는 오류 메시지)
개발자를 위한 Python 입문

메서드

  • 메서드 = 특정 데이터 유형에만 사용할 수 있는 함수

  • str 메서드

    • 입력 표준화 또는 텍스트 변환
# Calling a string method
string_variable.method()
개발자를 위한 Python 입문

문자열의 일부 바꾸기

  • .replace(text_to_be_replaced, text_to_change_it_to)
welcome_message = "Welcome to the recipe scaler, George"

welcome_message = welcome_message.replace("George", "John")

print(welcome_message)
Welcome to the recipe scaler, John
개발자를 위한 Python 입문

대소문자 변경하기

  • 이메일과 같은 사용자 입력 표준화
ingredient_name = "Basil Leaves"

# Convert to lowercase ingredient_name = ingredient_name.lower() print(ingredient_name)
basil leaves
# Change to uppercase
ingredient_name = ingredient_name.upper()
print(ingredient_name)
BASIL LEAVES
개발자를 위한 Python 입문

연습해 봅시다!

개발자를 위한 Python 입문

Preparing Video For Download...