Text Type: | str |
Numeric Types: | int, float, complex |
Sequence Types: | list, tuple, range |
Mapping Type: | dict |
Set Types: | set, frozenset |
Boolean Type: | bool |
Binary Types: | bytes, bytearray, memoryview |
None Type: | NoneType |
String
s1 = 'Hello World'
print(type(s1)) # <class 'str'>
print(s1)
s2 = "Python is fun"
print(type(s2)) # <class 'str'>
print(s2)
s3 = '''Life is too short, you need python.'''
print(type(s3)) # <class 'str'>
print(s3)
s4 = """Life is too short, you need python."""
print(type(s4)) # <class 'str'>
print(s4)
Unlike other languages, in Python, you can add and multiply the strings.
head = 'Python'
tail = ' is fun'
str = head + tail
print(str)
print(head + tail)
s = 'python'
print(s * 2)
print(s * 3)
print('='*50)
print('My Program')
print('='*50)
print('*'*1)
print('*'*2)
print('*'*3)
print('*'*4)
print('*'*5)
print('*'*5)
print('*'*4)
print('*'*3)
print('*'*2)
print('*'*1)
Indexing
Indexing is used when you need to extract parts of the data.
The first letter is index'0', and the last letter is index'-1'.
s = 'Life is too short, you need python'
print(s)
print(s[0]) # L
print(s[3]) # e
print(s[-1]) # n
print(s[-2]) # o
str = 'korea'
print(str[0]) # k
print(str[-2]) # e
Slicing
Example1
# format : variable[ start : stop ]
s = 'Life is too short, you need python'
print(s[0:2]) # 0 - 1 : Li
print(s[0:5]) # 0 - 4 : Life
print(s[5:7]) # 5 - 6 : is
print(s[12:17]) # 12 - 16 : short
print(s[ : 7]) # 0 -6 : Life is
print(s[5 : ]) # 5 - last : is too short, you need python
print(s[ : ]) # first - last
str = '20201024Rainy'
date = str[:8] # 20201024
weather = str[8:] # Rainy
print(date,':', weather)
year = str[:4] # 2020
month = str[4:6] # 10
day = str[6:8] # 24
print(year, 'Year', month, 'Month', day, 'Day')
Example2
str = 'korea'
print(str[0]) # [0] : k
print(str[-2]) # The second last : e
print(str[1:3]) # 1 - 2 : or
print(str[0:5:2]) # 0 - 4 (+2) : kra
print(str[:-1]) # first - -2 : kore
print(str[::-1]) # reverse : aerok
Example3
To change strings with slicing, you need to slice first and combine the letters.
s = 'pithon'
print(s[1]) # i
# i -> y
# s[1] = 'y' # TypeError: 'str' object does not support item assignment
s = s[:1] + 'y' + s[2:] # python
print(s)
Functions
upper() / lower()
txt = 'A lot of things happen everyday.'
result1 = txt.upper()
result2 = txt.lower()
print('result1:', result1) #result1: A LOT OF THINGS HAPPEN EVERYDAY.
print('result2:', result2) #result2: a lot of things happen everyday.
format()
%s | String |
%c | Character |
%d | Integer |
%f | float |
%% | % |
txt1 = 'Java'; txt2 = 'Python'
num1 = 5; num2 = 10
print('I like %s and %s!' %(txt1, txt2))
print('%s is %d times easier than %s.' %(txt2,num1,txt1))
print('%d + %d = %d' %(num1, num2, num1+num2))
print('Inflation rate in 2022: %d%%' %num1)
len()
msg = input('Insert a sentence.')
print(type(msg))
len = len(msg)
print('The length of the sentence is ', len,'.')
print('The length of the sentence is {}.'.format(len))
print('The length of the sentence is %d.'%len)
count()
txt = 'A lot of things occur each day, every day.'
count1 = txt.count('o')
count2 = txt.count('day')
count3 = txt.count(' ')
print('count1:', count1)
print('count2:', count2)
print('count3:', count3)
escape()
\n | enter |
\t | tab |
\\ | print"\" |
\' | print"'" |
\" | print""" |
print('I love Python.\n Python is easier than other languages!')
print('Name:John Simth\tSex:Male\tAge:22')
print('\"Welcome to Meadow\'s devlog!\"')
print('\'python\"')
print('\\Java\\')
find()
txt = 'Today is Tuesday! I love Tuesdays.'
offset1 = txt.find('e') # first 'e'
offset2 = txt.find('day') # first 'day'
offset3 = txt.find('day', 20) # 'day' after index[20]
offset4 = txt.find('j') # no 'j' => returns -1
print('offset1:', offset1) #11
print('offset2:', offset2) #2
print('offset3:', offset3) #29
print('offset4:', offset4) #-1
strip()
txt = ' I am hungry! '
result1 = txt.lstrip()
result2 = txt.rstrip()
result3 = txt.strip()
print('<'+txt+'>') #< I am hungry! >
print('<'+result1+'>') #<I am hungry! >
print('<'+result2+'>') #< I am hungry!>
print('<'+result3+'>') #<I am hungry!>
isdigit()
txt1 = '010-1234-5678'
txt2 = 'R2D2'
txt3 = '1212'
result1 = txt1.isdigit()
result2 = txt2.isdigit()
result3 = txt3.isdigit()
print('result1:', result1) # False
print('result2:', result2) # False
print('result3:', result3) # True
'Python' 카테고리의 다른 글
Python) Control statements (0) | 2022.11.06 |
---|---|
Python) Dictionary (0) | 2022.11.04 |
Python) Tuple / Set (0) | 2022.11.03 |
Python) Variables and Operators (0) | 2022.10.29 |
Python) Configuration and Basics (0) | 2022.10.27 |