코드로 우주평화

초보자를 위한 파이썬 300제 따라치기 #3. 파이썬 문자열 본문

나는 이렇게 학습한다/Language

초보자를 위한 파이썬 300제 따라치기 #3. 파이썬 문자열

daco2020 2021. 7. 26. 23:12
반응형

 오늘 배운 것 

>>> a = "3"
>>> b = "4"
>>> print(a+b)
34
>>> 
>>> print("HI"*3)
HIHIHI
>>> 
>>> print("-"*80)
--------------------------------------------------------------------------------
>>> 
>>> 
>>> t1 = 'python'
>>> t2 = 'java'
>>> 
>>> t3 = (t1 + t2) * 3
>>> 
>>> t3
'pythonjavapythonjavapythonjava'
>>> print( t3)
pythonjavapythonjavapythonjava
>>> 
>>> 
>>> name1 = "김민수"
>>> age1 = 10
>>> name2 = "이철희"
>>> age2 = 13
>>> 
>>> 이름: % 나
SyntaxError: invalid syntax
>>> 
>>> 

>>> 
>>> 
>>> 

>>> 
>>> print("이름: %s 나이: %d" % (name1, age1))
이름: 김민수 나이: 10
>>> print("이름: %s 나이: %d" % (name2, age2))
이름: 이철희 나이: 13
>>> 
>>> 
>>> 36
36
>>> 
>>> print("이름: {} 나이: {}".format(name1, age1))
이름: 김민수 나이: 10
>>> 
>>> print("이름: {} 나이: {}".format(name2, age2))
SyntaxError: invalid syntax
>>> print("이름: {} 나이: {}".format(name2, age2))
이름: 이철희 나이: 13
>>> 
>>> 
>>> 
>>> 
>>> print(f"이름: {name1} 나이: {age1}")
이름: 김민수 나이: 10
>>> print(f"이름: {name2} 나이: {age2}")
이름: 이철희 나이: 13
>>> 
>>> 
>>> 상장주식수 = "5,969,782,550"
>>> 상장주식수.reduce(",")
Traceback (most recent call last):
  File "<pyshell#78>", line 1, in <module>
    상장주식수.reduce(",")
AttributeError: 'str' object has no attribute 'reduce'
>>> del 상장주식수(",")
SyntaxError: cannot delete function call
>>> 상장주식수.del(",")
SyntaxError: invalid syntax
>>> 
>>> 
>>> 컴마제거 = 상장주식수.replace(",","")
>>> 타입변환 = int(컴마제거)
>>> print(타입변환, type(타입변환))
5969782550 <class 'int'>
>>> 
>>> 
>>> 분기 = "2020/03(E) (IFRS연결)"
>>> 분기[0:7]
'2020/03'
>>> 
>>> date = "  삼성전자  "
>>> print(date.replace("  ",""))
삼성전자
>>> 
>>> data1 = data.strip()
Traceback (most recent call last):
  File "<pyshell#94>", line 1, in <module>
    data1 = data.strip()
NameError: name 'data' is not defined
>>> data1 = date.strip()
>>> date
'  삼성전자  '
>>> data1
'삼성전자'
>>> # strip -> 공백제거 메서드
>>> 
>>> 
>>> letters = 'python'
>>> lang = 'python'
>>> print(lang[0], lang[2])
p t
>>> license_plate = "24가 2210"
>>> print(license_plate(-4:))
SyntaxError: invalid syntax
>>> print(license_plate[-4:])
2210
>>> 
>>> string = "홀짝홀짝홀짝"
>>> print(string[0,0,-2])
Traceback (most recent call last):
  File "<pyshell#110>", line 1, in <module>
    print(string[0,0,-2])
TypeError: string indices must be integers
>>> print(string[0:0:-2])

>>> print(string[0:0:2])

>>> print(string[::2])
홀홀홀
>>> ??
SyntaxError: invalid syntax
>>> string = "PYTHON"
>>> string[-1:]
'N'
>>> string[-1:-6]
''
>>> string[::-1]
'NOHTYP'
>>> #오프셋에 대해 정확히 모르겠음...
>>> 
>>> 
>>> 
>>> phone_number = "010-1111-2222"
>>> print(phone_number.replace("-"," "))
010 1111 2222
>>> print(phone_number.replace("-",""))
01011112222
>>> 
>>> url
Traceback (most recent call last):
  File "<pyshell#127>", line 1, in <module>
    url
NameError: name 'url' is not defined
>>> url = "http://abc.co.kr"
>>> print(ur[-2])
Traceback (most recent call last):
  File "<pyshell#129>", line 1, in <module>
    print(ur[-2])
NameError: name 'ur' is not defined
>>> print(ur[-2:])
Traceback (most recent call last):
  File "<pyshell#130>", line 1, in <module>
    print(ur[-2:])
NameError: name 'ur' is not defined
>>> print(url[-2:])
kr
>>> 
>>> 
>>> 
>>> url.split = url.split('.')
Traceback (most recent call last):
  File "<pyshell#135>", line 1, in <module>
    url.split = url.split('.')
AttributeError: 'str' object attribute 'split' is read-only
>>> url_split = url.split('.')
>>> url_split
['http://abc', 'co', 'kr']
>>> print(url_split[-1])
kr
>>> 
>>> 
>>> 
>>> lang
'python'
>>> lang[0] = "p"
Traceback (most recent call last):
  File "<pyshell#143>", line 1, in <module>
    lang[0] = "p"
TypeError: 'str' object does not support item assignment
>>> 
>>> string = 'abcd'
>>> string1 = string.replace('a','A')
>>> string1
'Abcd'
>>> string
'abcd'
>>> 
>>> string
'abcd'
>>> string.replace('b','B')
'aBcd'
>>> string
'abcd'
>>> 
>>> 
>>> ticker = "btc_krw"
>>> ticker.upper()
'BTC_KRW'
>>> ticker
'btc_krw'
>>> upper = ticker.upper()
>>> upper
'BTC_KRW'
>>> lower = upper.lower()
>>> lower
'btc_krw'
>>> 
>>> 
>>> tomato = 'hello'
>>> print(tomato.capitalize())
Hello
>>> 
>>> 
>>> file_name = "보고서.jpg"
>>> a = file_name.endswith()
Traceback (most recent call last):
  File "<pyshell#169>", line 1, in <module>
    a = file_name.endswith()
TypeError: endswith() takes at least 1 argument (0 given)
>>> a = file_name.endswith(jpg)
Traceback (most recent call last):
  File "<pyshell#170>", line 1, in <module>
    a = file_name.endswith(jpg)
NameError: name 'jpg' is not defined
>>> a = file_name.endswith('jpg')
>>> a
True
>>> b
'4'
>>> 
>>> file_name.endswith('jpg'+'jpeg')
False
>>> file_name.endswith('jpg','mpeg')
Traceback (most recent call last):
  File "<pyshell#176>", line 1, in <module>
    file_name.endswith('jpg','mpeg')
TypeError: slice indices must be integers or None or have an __index__ method
>>> file_name.endswith(('jpg','mpeg'))
True
>>> file = "2020_보고서.wmv"
>>> file.startswith('2020')
True
>>> a = "hello world"
>>> a.split(" ")
['hello', 'world']
>>> ticker
'btc_krw'
>>> ticker.split("_")
['btc', 'krw']
>>> date
'  삼성전자  '
>>> date = "2020-05-01"
>>> print(date.split("-"))
['2020', '05', '01']
>>> date.split("-")
['2020', '05', '01']
>>> 
>>> data = "12313123   "
>>> data
'12313123   '
>>> data.rstrip()
'12313123'
>>> data = "   adad  sa"
>>> data.strip
<built-in method strip of str object at 0x000001935C0201B0>
>>> data.strip()
'adad  sa'
>>> data.mstrip()
Traceback (most recent call last):
  File "<pyshell#195>", line 1, in <module>
    data.mstrip()
AttributeError: 'str' object has no attribute 'mstrip'
>>> data.lstrip()
'adad  sa'
>>>

 


 

 오늘 느낀 것 

처음에는 문제를 풀어보려고 했는데 배운 것들을 다까먹어서 코드가 쳐지지 않았다. 그래서 아직 내가 초보자임을 인정 하고 일단 따라쳐보기로 했다. 한번씩 따라 쳐보다 보면 파이썬 자체가 익숙해지겠지

 

그래도 정답을 바로치기 보다는 생각난 것들은 쳐보고 틀리면 그때 정답을 따라치고 있다. 에러가 난 것도 오타가 난것도 블로그에 그대로 올린다. 나중에 내가 실력자가 되었을 때 이 글을 보면 어떤생각이 들까? 왠지 미래의 내 반응이 궁금하다.

 

- 문자열을 수정이 불가능하다

- 슬라이싱에서 오프셋은 반복하여 출력하는 것 [::2] > 문자열의 2칸씩을 출력한다

- 슬라이싱에서 오프셋으로 문자열을 뒤집는 것은 전체문자열의 오프셋을 -1 로 하면 된다. 이건 그냥 외우기

반응형