๐Ÿ“—
JunegLee's TIL
  • TIL
  • python
    • class
    • String Basic
    • regularExpression
    • String function
    • Generator
    • String format
    • getset
    • module
    • while
    • numpy
    • print()
    • matplotlib
    • for
    • Boolean
    • tuple
    • package
    • input(variable)
    • list
    • if
    • file
    • type()
    • pandas
    • function
    • dictionary
    • ๊ตฌ๋ฌธ ์˜ค๋ฅ˜์™€ ์˜ˆ์™ธ
    • builtinFunction
    • Constructor
  • algorithm
    • sort
      • mergeSort
      • insertionSort
      • bubbleSort
      • heapSort
      • quickSort
      • selectionSort
    • recursion
    • Greedy
    • DepthFirstSearch
    • basic
      • DataStructure
    • hash
    • BreadthFirstSearch
  • tensorflow
    • keras
      • layers
        • Flatten
        • Flatten
        • Dense
        • Dense
        • Conv2D
        • Conv2D
    • tensorflow1x
    • tensorflow2x
  • DB
    • setting
    • join
    • subQuery
    • overview
  • deep-learning
    • neuralNetwork
    • perceptron
    • neuralNetworkLearning
    • convolution neural network
    • Gradient Descent
    • Linear Regression
    • backPropagation
    • logistic regression
    • overview
  • textPreprocessing
    • overview
  • java
    • basics
      • generic
      • Variable
      • String
    • theory
      • Object Oriented Programing
  • NLP
    • Embedding
    • Natural Language Processing
Powered by GitBook
On this page
  • String function
  • ๋ฌธ์ž ๊ฐœ์ˆ˜ ์„ธ๊ธฐ : count()
  • ๋ฌธ์ž์—ด ์ฐพ๊ธฐ_1 : find()์™€ rfind()
  • ๋ฌธ์ž์—ด ์ฐพ๊ธฐ_2 : index()
  • ๋ฌธ์ž์—ด ์‚ฝ์ž… : join()
  • ๋Œ€์†Œ๋ฌธ์ž ๋ฐ”๊พธ๊ธฐ : upper()์™€ lower()
  • ๋ฌธ์ž์—ด ๊ณต๋ฐฑ ์ œ๊ฑฐ : Strip()
  • ๋ฌธ์ž์—ด ๋ฐ”๊พธ๊ธฐ : replace()
  • ๋ฌธ์ž์—ด์˜ ๊ตฌ์„ฑ ํŒŒ์•…ํ•˜๊ธฐ: isOO()
  • ๋ฌธ์ž์—ด๊ณผ in ์—ฐ์‚ฐ์ž
  • ๋ฌธ์ž์—ด ์ž๋ฅด๊ธฐ : split()
  • map์„ ์‚ฌ์šฉํ•˜์—ฌ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜

Was this helpful?

  1. python

String function

String function

๋ฌธ์ž ๊ฐœ์ˆ˜ ์„ธ๊ธฐ : count()

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

๋ฌธ์ž์—ด ์ฐพ๊ธฐ_1 : find()์™€ rfind()

ํ•จ์ˆ˜

์„ค๋ช…

find()

์™ผ์ชฝ์—์„œ ์ฐพ์•„์„œ ์ฒ˜์Œ ๋“ฑ์žฅํ•˜๋Š” ์œ„์น˜๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.

rfind()

์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ์ฐพ์•„์„œ ์ฒ˜์Œ ๋“ฑ์žฅํ•˜๋Š” ์œ„์น˜๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.

c = "์•ˆ๋…•ํ•˜์„ธ์š”".find("์•ˆ๋…•") 
print(c) # 0 
# ๋ฌธ์ž์—ด์€ 0๋ฒˆ์งธ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ๋…•์€ 0๋ฒˆ์จฐ ์œ„์น˜๋กœ ์ฐพ์€ ๊ฒƒ์ด๋‹ค
d = "์•ˆ๋…•์•ˆ๋…•ํ•˜์„ธ์š”".rfind("์•ˆ๋…•")
print(d) # 2 
# ์˜ค๋ฅธ์ชฝ์—์„œ๋ถ€ํ„ฐ ์ฐพ์•˜๊ธฐ ๋•Œ๋ฌธ์— 0 1 2์ธ 2๋ฒˆ์งธ ์ธ๊ฒƒ์ด๋‹ค

๋ฌธ์ž์—ด ์ฐพ๊ธฐ_2 : index()

a = "Life is too short"
print(a.index('t')) # 8
# ๋งŒ์•ฝ ์ฐพ๋Š” ๋ฌธ์ž๋‚˜ ๋ฌธ์ž์—ด์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ

๋ฌธ์ž์—ด ์‚ฝ์ž… : join()

>>> ",".join('abcd')
'a,b,c,d'

>>> ",".join(['a', 'b', 'c', 'd'])
'a,b,c,d'

๋Œ€์†Œ๋ฌธ์ž ๋ฐ”๊พธ๊ธฐ : upper()์™€ lower()

a = "Hello Python Programing"
print(a.upper()) # HELLO PYTHON PROGRAMING
print(a.lower()) # hello python programing

๋ฌธ์ž์—ด ๊ณต๋ฐฑ ์ œ๊ฑฐ : Strip()

: strip()์„ ์‚ฌ์šฉํ•˜๋ฉด ์–‘์ชฝ ๊ณต๋ฐฑ์„ ์ง€์šฐ๋Š” ๊ฒƒ์ด๋‹ค

b = """
    ์•ˆ๋…•ํ•˜์„ธ์š” 
๊ณต๋ฐฑ ์ œ๊ฑฐ ํ•ฉ๋‹ˆ๋‹ค
"""
print(b.strip())
  • ์™ผ์ชฝ ๊ณต๋ฐฑ ์ง€์šฐ๊ธฐ : lstrip()

    >>> a = " hi "
    >>> a.lstrip()
    'hi '
  • ์˜ค๋ฅธ์ชฝ ๊ณต๋ฐฑ ์ง€์šฐ๊ธฐ : rstrip()

    >>> a= " hi "
    >>> a.rstrip()
    ' hi'

๋ฌธ์ž์—ด ๋ฐ”๊พธ๊ธฐ : replace()

: replace(๋ฐ”๋€Œ๊ฒŒ ๋  ๋ฌธ์ž์—ด, ๋ฐ”๊ฟ€ ๋ฌธ์ž์—ด)

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

๋ฌธ์ž์—ด์˜ ๊ตฌ์„ฑ ํŒŒ์•…ํ•˜๊ธฐ: isOO()

ํ•จ์ˆ˜

์„ค๋ช…

isalnum()

๋ฌธ์ž์—ด์ด ์•ŒํŒŒ๋ฒณ ๋˜๋Š” ์ˆซ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ

isalpha()

๋ฌธ์ž์—ด์ด ์•ŒํŒŒ๋ฒณ์œผ๋กœ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ

isidentifier()

๋ฌธ์ž์—ด์ด ์‹๋ณ„์ž๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ

isdecimal()

๋ฌธ์ž์—ด์ด ์ •์ˆ˜๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ

isdigit()

๋ฌธ์ž์—ด์ด ์ˆซ์ž๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ

isspace()

๋ฌธ์ž์—ด์ด ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ

islower()

๋ฌธ์ž์—ด์ด ์†Œ๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ

isuppser()

๋ฌธ์ž์—ด์ด ๋Œ€๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ

print("trainA10".isalnum()) # True
print("10".isdigit()) # True

๋ฌธ์ž์—ด๊ณผ in ์—ฐ์‚ฐ์ž

: ๋ฌธ์ž์—ด ๋‚ด๋ถ€์— ์–ด๋–ค ๋ฌธ์ž์—ด์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ ค๋ฉด in ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค

print("์•ˆ๋…•" in "์•ˆ๋…•ํ•˜์„ธ์š”") # True
print("๋ฉ”๋กฑ" in "์•ˆ๋…•ํ•˜์„ธ์š”") # False

๋ฌธ์ž์—ด ์ž๋ฅด๊ธฐ : split()

e = "10 20 30 40 50".split()
print(e) # ['10', '20', '30', '40', '50'] # ์‹คํ–‰๊ฒฐ๊ณผ๋กœ ๋ฆฌ์ŠคํŠธ(list)๊ฐ€ ๋‚˜์˜ด
  • ์ž…๋ ฅ ๊ฐ’์„ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜

    f, g = input('์ˆซ์ž๋ฅผ ๋‘ ๊ฐœ ์ž…๋ ฅํ•˜์„ธ์š” : ').split()
    f = int(f)
    g = int(g)
    print(f + g) # 10 20 ์ž…๋ ฅ 30 ์ถœ๋ ฅ

map์„ ์‚ฌ์šฉํ•˜์—ฌ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜

h, i = map(int, input('์ˆซ์ž๋ฅผ ๋‘ ๊ฐœ ์ž…๋ ฅํ•˜์„ธ์š” : ').split())
print(h + i)
PreviousregularExpressionNextGenerator

Last updated 4 years ago

Was this helpful?