๊ด€๋ฆฌ ๋ฉ”๋‰ด

C-log

Welcome to Python : ๋ฌธ์ž์—ด ์ž๋ฃŒํ˜• ๋ณธ๋ฌธ

๐Ÿ“˜Python

Welcome to Python : ๋ฌธ์ž์—ด ์ž๋ฃŒํ˜•

4:Bee 2023. 7. 6. 14:26
728x90

Title Code Console
๋ฌธ์ž์—ด ์ž๋ฃŒํ˜•
๊ธฐ๋ณธ ๋ฌธ์ž์—ด ๊ตฌ์กฐ

"Hello World"
'Python is fun'

 
๋”ฐ์˜ดํ‘œ๋ฅผ ์ด์šฉํ•œ ์ค„ ๋ฐ”๊ฟˆ

multiline
= '''
Life is too short
You need python
'''
print(multiline)

Life is too short
You need python
๋ฌธ์ž์—ด ์ธ๋ฑ์‹ฑ

a
= "Life is too short, You need python"
print(a[0])

L
๋ฌธ์ž์—ด ์Šฌ๋ผ์ด์‹ฑ

a
= "Life is too short, You need python"
slice = a[0:4]
print(slice)

Life

a
= "Life is too short, You need python"
slice = a[:8]
print(slice)

Life is

a
= "Life is too short, You need python"
slice = a[5:7]
print(slice)

is
๋ฌธ์ž์—ด ํฌ๋งคํŒ…

format
= "I eat %d apples." % 3
print(format)

I eat 3 apples.

format
= "I eat %s apples." % "five"
print(format)

I eat five apples.
๋ฌธ์ž์—ด ์ •๋ ฌ๊ณผ ๊ณต๋ฐฑ


print
("%10s" % "hi")


'        hi'


print
("%-10sjane." % "hi")


hi        jane.


print
("%0.4f" % 3.42134234)


3.4213


print
("I eat {0} apples".format(3))


I eat 3 apples


print
("{0:<10}".format("hi"))


'hi        '


print
("{0:>10}".format("hi"))


'        hi'


print
("{0:=^10}".format("hi"))


====hi====

y
= 3.42134234
print("{0:10.4f}".format(y))

'    3.4213'

name
= 'ํ™๊ธธ๋™'
age = 30
print(f'๋‚˜์˜ ์ด๋ฆ„์€ {name}์ž…๋‹ˆ๋‹ค. ๋‚˜์ด๋Š” {age}์ž…๋‹ˆ๋‹ค.')

3.6ver ์ด์ƒ๋ถ€ํ„ฐ
๋‚˜์˜ ์ด๋ฆ„์€ ํ™๊ธธ๋™์ž…๋‹ˆ๋‹ค. ๋‚˜์ด๋Š” 30์ž…๋‹ˆ๋‹ค.
Title Code Console
๋ฌธ์ž์—ด ๊ด€๋ จ ํ•จ์ˆ˜
count

a
= "hobby"
print(a.count('b'))

2
find

a
= "Python is the best choice"
find_b = a.find('b')
find_k = a.find('k')
print(find_b)
print(find_k)

14
-1
index

a
= "Python is the best choice"
find_b = a.index('b')
find_k = a.index('k')
print(find_b)
print(find_k)

Traceback (most recent call last):
  File "...Quiz.py", line 3, in <module>
    find_k = a.index('k')
ValueError: substring not found
find์™€ index์˜ ์ฐจ์ด find๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ฌธ์ž์—ด์„ ๊ฒ€์ƒ‰๋˜๋ฉด '-1'์„ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ index๋Š” error๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
join

toJoin
= ",".join('abcd')
print(toJoin)

a,b,c,d
upper

a
= "hi"
print(a.upper())

HI
lower

a
= "HI"
print(a.lower())

hi
lstrip

a
= " hi "
print(a.lstrip())

'hi '
rstrip

a
= " hi "
print(a.rstrip())

' hi'
strip

a
= " hi "
print(a.strip())

'hi'
replace

a
= "Life is too short"
print(a.replace("Life", "Your leg"))

Your leg is too short
split

a
= "Life is too short"
print(a.split())
b = "a:b:c:d"
print(b.split(':'))

['Life', 'is', 'too', 'short']
['a', 'b', 'c', 'd']

 

728x90
Comments