a = 456
b = str(a)
print(b) #456
print(type(b)) #<class 'str'>
if 'A' == 65:
print('O')
else:
print("X")
# X
if 'A' == chr(65):
print('O')
else:
print("X")
# O
x = 'a'
print(ord(x)) #97
print(ord('x')) #120
print(type(ord('x'))) #int
print(chr(65)) #A
print(chr(48)) #0
print(type(chr(48))) #<class 'str'>
name = "david"
print(f"{name:*<10}")
num = 17.5678
print(f"{num:.2f}")
print(f"{num:010.3f}")
# david*****
# 17.57
# 000017.568
str = "python"
my_list = list(str)
print(my_list)
# ['p', 'y', 't', 'h', 'o', 'n']
str = 'Hello'
my_list = list(str[-1::-1])
# list(str[::-1])
print(my_list)
# ['o', 'l', 'l', 'e', 'H']
str = "python is good "
my_list = str.split()
print(my_list)
# ['python', 'is', 'good']
str = "a,b,c,d,e"
my_list = str.split(',')
print(my_list)
# ['a', 'b', 'c', 'd', 'e']
def listToString(str_list):
result = ""
for s in str_list:
result += s + " "
return result.strip()
str_list = ['This', 'is', 'a', 'python tutorial']
result = listToString(str_list)
print(result)
# This is a python tutorial
str_list = ['There', 'is', 4, "items"]
result = ' '.join(str(s) for s in str_list)
print(result)
str_list = ['There', 'is', 4, "items"]
result = ' '.join(s for s in str_list)
print(result)
str_list = [3, 4, 5, 6]
result = ''.join(str(s) for s in str_list)
print(int(result))
l = ['b', 'a', 'c']
s = ''.join(l)
print(s) #bac
print(type(s)) #<class 'str'>
l = ['apple', 'can', 'fly']
s = ''.join(l)
print(s) #applecanfly
print(type(s)) #<class 'str'>
s = "abcdefg"
if "de" in s:
print("yes")
#yes
text = 'Welcome to Codetorial'
pos_e = text.find('e')
print(pos_e) #1
pos_Code = text.find('Code')
print(pos_Code) #11
pos_code = text.find('code')
print(pos_code) #-1
text = 'Welcome to Codetorial'
pos_e = text.index('e')
print(pos_e) # 1
pos_Code = text.index('Code')
print(pos_Code) #11
pos_code = text.index('code')
print(pos_code) #ValueError: substring not found
text = 'Welcome to Codetorial'
pos_to_last = text.rfind('to')
print(pos_to_last) #15
pos_to_first = text.find('to')
print(pos_to_first) #8
text = 'Welcome to Codetorial'
pos_Code_last = text.rindex('Code')
print(pos_Code_last) #11
pos_code_last = text.rindex('code')
print(pos_code_last) #ValueError: substring not found
: 함수를 적용한 기존 문자열 자체의 값은 유지된다. 함수 적용한 결과의 새로운 문자열을 리턴
text = 'abccc'
text.replace('ab', '')
print(text)
# abccc
text = 'Welcome to Codetorial'
new_text = text.replace('to', 'TO')
print(new_text)
# Welcome TO CodeTOrial
text = "this is a abcabcabc text"
text.replace("abc", "")
# 'this is a text'
text.replace("abc", "").replace("a ","a")
# 'this is a text'
: 문자열 양쪽 끝에서 삭제
: 디폴드 - 인자를 전달하지 않으면 문자열 양 끝의 white space 제거
s = " abc d ef "
print('-'+s.strip()+'-')
# -abc d ef-
s = ",,,,,123.....water....pp"
print(s.strip(',123.p'))
# water
import re
text = "abc123def456ghi"
new_text = re.sub(r"[a-z]", "", text)
print(new_text)
# 123456
s = "abcd"
i = 1
l = list(s)
del l[i]
s = ''.join(l)
print(s) #acd
s = "abcd"
i = 1
s = s[:i] + s[i+1:]
print(s) #acd
s = "abcde"
print(s[-1:]) #e
print(s[2:]) #cde
print(s[2:-1]) # cd
s = "badfgcaa"
l = sorted(s)
print(s) #badfgcaa
print(l) #['a', 'a', 'a', 'b', 'c', 'd', 'f', 'g']
str1 = 'abcde'
str1_list = list(str1) # ['a','b','c','d','e']
str1_list.reverse() # ['e','d','c','b','a']
print(''.join(str1_list)) # edcba
str1 = 'abcde'
print(str1[::-1]) # 'edcba'
s = "sDaXw"
s2 = s.upper()
print(s) #sDaXw
print(s2) #SDAXW
s = "sDaXw"
s2 = s.upper()
print(s.isupper()) #False
print(s2.isupper()) #True