データ構造でーたこうぞう:文字列もじれつ

4403 ワード

文字列(String)

  • 出力フォーマット
    -endオプション値出力
    使用-%出力
    -使用する出力
    -f文字列フォーマット
    Ex. end option
    ```
    print("Hello Python", end='')
    print("Hello Python", end='\n') 	# default : end='\n' 
    ```
    出力値
    ```
    Hello PythonHello Python
    ```
    Ex. using %
    ```
    # %s(str) / %f(float) / %d(integer) / %c(character)
    # -------------------
    print("Hello! %s" %('James'))
    print("%.3f" %(3.1415))
    print("%d" %(12.5))
    print("%c" %('a'))
    ```
    出力値
    ```
    Hello! James
    3.142
    12
    a
    ```
    Ex. using {}
    ```
    print("Welcome to {}".format('파이썬'))
    print("Welcome to {}".format('3.14'))
    print("Welcome to {}".format('12'))
    print("Welcome to {}".format('w'))
    print("Welcome to {} {} {}".format('파이썬', 3.14, '아'))
    print("my favorite movie is {:03d}".format(7))
    print("원주율은 ? {:.2f}".format(3.141592))
    ```
    出力値
    ```
    Welcome to 파이썬
    Welcome to 3.14
    Welcome to 12
    Welcome to w
    Welcome to 파이썬 3.14 아
    my favorite movie is 007
    원주율은 ? 3.14
    ```
    Ex. f formating
    ```
    name = John
    age = 15
    PI = 3.141592
    print(f"My name is {name}, and I'm {age}years old.")
    print(f"PI = {PI:.2f}"
    ```    
    出力値
    ```
    My name is John, and I'm 15years old.
    PI = 3.14
    ```
  • indexing/slicing
    - index order starts at '0'
    - index reverse order starts at '-1'

    Ex.
    ```
    my_str = "python"
    # -------------------
    # indexing
    # my_str[0] = p
    # my_str[4] = o
    # my_str[-1] = n
    # -------------------
    # slicing
    # my_str[1:4] = yth
    # my_str[:] = python
    # my_str[2:] = thon
    # my_str[:4] = pyth
    # my_str[-2:] = on
    # my_str[-4:-1] = tho
    # my_str[-4:1] = ' ' space
    # my_str[0:6:2] = pto ; step = 2
    # my_str[::2] = pto ; :: means all, and step = 2
    # my_str[::-1] = nohtyp ; step = -1 means reverse step 1
    ```
  • Str functions 1) split, join
    - split() : use it when you want to split the str
    - join() : use it when you want to join the list
    Ex.
    ```
    movies = "미나리, 경계선, 기생충, 학교"
    # ------------------------------------------------
    movieList = movies.split(",")
    # movieList = ['미나리', '경계선', '기생충', '학교']
    # ------------------------------------------------
    movie = ''.join(movieList)
    # movie = "미나리 경계선 기생충 학교"
    ```
  • Str functions 2) lower, upper
    - lower() : makes all characters in the string lowercase
    - upper() : makes all characters in the string uppercase
    Ex.
    ```
    a = "This is an apple"
    #
    print(a.lower())	# this is an apple
    print(a.upper()) 	# THIS IS AN APPLE
    ```
  • Str functions 3) startswith, endswith
    - startswith("==") : return T/F : if str starts with "=="
    - endswith("==") : return T/F : if str ends with "=="
    Ex.
    ```
    a = "01_star.png"
    #
    print(a.startswith("01"))	# True
    print(a.endswith("png"))	# True
    ```
  • Str functions 4) replace
    - replace("==", "--") : replace "=="(in the string) with "--"
    Ex.
    ```
    a = "01_sample.png"
    a = a.replace("png", "mp3")
    # a = "01_sample.mp3"
    ```
  • Str functions 5) lstrip, rstrip, strip
    - lstrip() : remove left-side spaces in the string
    - rstrip() : remove right-side spaces in the string
    - strip() : remove both-side spaces in the string
    Ex.
    ```
    a = "     01_sample.png"
    b = "01_sample.png     "
    c = "     01_sample.png     "
    #
    a = a.lstrip()	# a = "01_sample.png"
    b = b.rstrip()	# b = "01_sample.png"
    c = c.strip()	# c = "01_sample.png"
    ```