백준-문자열-정리

Posted by : on

Category : PS


문자열 관련 문제 해결 시 자주 사용되는 유형 및 코드 예시

1. 문자열 순회 및 조작

  • 문자열 순회: 문자열의 각 문자를 순차적으로 방문하는 과정.
    s = "hello"
    for char in s:
        print(char)
    
  • 문자열 뒤집기: 문자열을 거꾸로 만드는 과정.
    s = "hello"
    reversed_s = s[::-1]
    

2. 부분 문자열 및 패턴 매칭

  • 부분 문자열 찾기: 특정 문자열 내에서 다른 문자열이 등장하는 위치를 찾는 과정.
    s = "hello world"
    index = s.find("world") #index = 6
    
  • 정규 표현식 사용하기: 복잡한 문자열 패턴을 찾거나 매칭할 때 사용.
    import re
    s = "example 123 test 456"
    numbers = re.findall(r'd+', s) #['123', '456']
    

3. 문자열 분할 및 합치기

  • 문자열 분할(split): 문자열을 특정 구분자로 나누어 여러 부분으로 분할
    s = "one, two, three"
    parts = s.split(",") #['one', 'two', 'three']
    
  • 문자열 합치기(join): 여러 문자열을 하나로 결합.
    parts = ['one', 'two', 'three']
    s = ",".join(parts) #one,two,three
    

4. 문자열의 각 문자 다루기

  • 문자열에서의 대소문자 변환
    s = "Hello World"
    lower_s = s.lower() #"hello world"
    upper_s = s.upper() #"HELLO WORLD"
    
  • 문자열에서 특정 문자 카운팅 및 제거
    s = "hello World"
    count = s.count('l') #3
    removed_s = s.replace('l', '') #"Heo word"
    

5. 문자열 포매팅

  • 문자열 내 변수 사용: 문자열 내에서 변수의 값을 동적으로 삽입
    name = "John"
    age = 30
    greeting = f"Hello, {name}. You are {age} years old."
    

About Sejun Jeong
Sejun Jeong

Seize the day with programming

Email : usopked16496@gmail.com

Website : https://usopked.github.io

About UPKED

2024년
2025년

Star
Categories
Useful Links