/ PROGRAMING

W3school_pythontutorial(2)-String_Methods

W3school에서 python 내실 다지기 목록

W3school_pythontutorial(2)-String_Methods

내실 다지기 : w3school 사이트에서 python tutorial을 처음부터 끝까지 하는 목표로 시작하는 포스팅으로 제공해주는 목차 순서대로 진행한다. 간단한 내용은 제공해준 예제만 실행해본다. 활용해볼 예제는 추가로 간단한 예제를 만들어 실행하는 코드까지 작성하며 내실을 다진다.

String Methods

Python - String Methods 내실 다지기 이므로 모든 함수를 실행해본다.

string.capitalize()

  • 문자열의 첫 단어를 대문자로 바꾼다.
txt = "hello, and welcome to my world."

x = txt.capitalize()

print (x)
Hello, and welcome to my world.

string.casefold()

txt = "Hello, And Welcome To My World!"

x = txt.casefold()

print(x)
hello, and welcome to my world!

string.center(length, character)

txt = "banana"

x = txt.center(20)

print(x)
       banana       
txt = "banana"

x = txt.center(20, "O")

print(x)
OOOOOOObananaOOOOOOO

string.count(value, start, end)

  • 해당 문자열에서 특정 단어가 몇개 있는지 확인할 수 있는 함수
txt = "I love apples, apple are my favorite fruit"

x = txt.count("apple")

print(x)
2
txt = "I love apples, apple are my favorite fruit"

x = txt.count("apple", 10, 24)

print(x)
1

string.encode(encoding=encoding, errors=errors)

  • Default is UTF-8 문자열을 인코딩 한다.
txt = "My name is Ståle"

x = txt.encode()

print(x)
b'My name is St\xc3\xa5le'
txt = "My name is Ståle"

print(txt.encode(encoding="ascii",errors="backslashreplace"))
print(txt.encode(encoding="ascii",errors="ignore"))
print(txt.encode(encoding="ascii",errors="namereplace"))
print(txt.encode(encoding="ascii",errors="replace"))
print(txt.encode(encoding="ascii",errors="xmlcharrefreplace"))
b'My name is St\\xe5le'
b'My name is Stle'
b'My name is St\\N{LATIN SMALL LETTER A WITH RING ABOVE}le'
b'My name is St?le'
b'My name is Ståle'

string.endswith(value, start, end)

  • 문자열이 지정된 값으로 끝나면 True를 반환하고 그렇지 않으면 False를 반환합니다.
txt = "Hello, welcome to my world."

x = txt.endswith(".")

print(x)
True
txt = "Hello, welcome to my world."

x = txt.endswith("my world.")

print(x)
True
txt = "Hello, welcome to my world."

x = txt.endswith("my world.", 5, 11)

print(x)
False

string.expandtabs(tabsize)

  • 탭의 크기를 조절해준다.
txt = "H\te\tl\tl\to"

x =  txt.expandtabs(2)

print(x)
H e l l o
txt = "H\te\tl\tl\to"

print(txt)
print(txt.expandtabs())
print(txt.expandtabs(2))
print(txt.expandtabs(4))
print(txt.expandtabs(10))
H	e	l	l	o
H       e       l       l       o
H e l l o
H   e   l   l   o
H         e         l         l         o

string.find(value, start, end)

  • 특정 문자열이 어디 있는지 확인, 오류시 -1 출력
txt = "Hello, welcome to my world."

x = txt.find("welcome")

print(x)
7

string.index(value, start, end)

  • 특정 문자열이 어디 있는지 확인, 오류시 에러 출력
txt = "Hello, welcome to my world."

x = txt.index("e")

print(x)
1
txt = "Hello, welcome to my world."

print(txt.find("q"))
print(txt.index("q"))
-1



---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

Input In [63], in <cell line: 4>()
      1 txt = "Hello, welcome to my world."
      3 print(txt.find("q"))
----> 4 print(txt.index("q"))


ValueError: substring not found

string.format(value1, value2…)

  • 양이 많으므로 따로 추후 블로그에 다룬다.

string.isalnum()

  • 문자열이 숫자와 알파벳으로만 이루어졌는지 확인하는 함수
txt = "Company12"

x = txt.isalnum()

print(x)
True
txt = "Company 12" # 빈칸이 들어가 있어서 False

x = txt.isalnum()

print(x)
False

string.isalpha()

  • 알파벳으로 이루어져있는지 확인
txt = "CompanyX"

x = txt.isalpha()

print(x)
True
txt = "Company10"

x = txt.isalpha()

print(x)
False

string.isdecimal()

  • 유니코드 문자열에서만 사용되며 모든 문자가 소수(0-9)이면 True를 반환합니다.
txt = "\u0033" #unicode for 3

x = txt.isdecimal()

print(x)
True
a = "\u0030" #unicode for 0
b = "\u0047" #unicode for G

print(a.isdecimal())
print(b.isdecimal())
True
False

string.isdigit()

  • 숫자인지 확인
txt = "50800"

x = txt.isdigit()

print(x)
True
a = "\u0030" #unicode for 0
b = "\u00B2" #unicode for ²

print(a.isdigit())
print(b.isdigit())
True
True

string.isidentifier()

  • 문자열이 유효한 식별자이면 True를 반환하고 그렇지 않으면 False를 반환
  • 유효한 식별자 영숫자(az) 및 (0-9) 또는 밑줄(_)만 포함하는 경우 유효한 식별자, 숫자로 시작하거나 공백을 포함할 수 없습니다.
txt = "Demo"

x = txt.isidentifier()

print(x)
True
a = "MyFolder"
b = "Demo002"
c = "2bring"
d = "my demo"

print(a.isidentifier())
print(b.isidentifier())
print(c.isidentifier())
print(d.isidentifier())
True
True
False
False

string.islower()

  • 알파벳 문자열을 소문자로 바꾸어준다.
txt = "hello world!"

x = txt.islower()

print(x)
True
a = "Hello world!"
b = "hello 123"
c = "mynameisPeter"

print(a.islower())
print(b.islower())
print(c.islower())
False
True
False

string.isnumeric()

  • 숫자로만 이루어져있는지, 음수 소수도 안된다.
txt = "565543"

x = txt.isnumeric()

print(x)
True
a = "\u0030" #unicode for 0
b = "\u00B2" #unicode for &sup2;
c = "10km2"
d = "-1"
e = "1.5"

print(a.isnumeric())
print(b.isnumeric())
print(c.isnumeric())
print(d.isnumeric())
print(e.isnumeric())
True
True
False
False
False