10 Python String Methods With Usage

Python string methods help you work with strings. These ten methods mostly people ask in interviews and popular as well. Here’s my previous post on 7 daily use python string functions.

Advertisements

Here’s a fact on method and function in Python – Maybe it’s an interview question. People often get confused between a method and a function. The fact is, in Python, there is a difference between these two.

Functions have parenthesis and arguments, and when these functions get associated with an object, they become a method.

Python string methods

List of inbuilt string methods

  1. help()
  2. find()
  3. upper()
  4. lower()
  5. strip()
  6. replace()
  7. split()
  8. join()
  9. in and not in (These are membership operators and not methods)
  10. endswith()

#1: help()

a=”my own string”

b=help(a.upper)
print(b)

Result: None

#2: find()

a=”my own string”

b=a.find(‘own’)
print(b)

Result: 3 (index)

#3: upper()

a=”my own string”

b=a.upper()
print(b

Result: MY OWN STRING (converts to upper case)

#4: lower()

a=”my OWN string”

b=a.lower()
print(b)

Result: my own string (converts to lower)

#5: strip()

a=”my Own String”

b=a.strip(‘my’)
print(b)

Resut: Own String (it strips only edges)

#6: replace()

a=”my Own String”

b=a.replace(‘my’, ‘This is’)
print(b)

Result: This is Own String (it replaces a portion of string)

#7: split()

a = ‘My Own String’
b= a.split()
print(b)

Result: [‘My’, ‘Own’, ‘String’]

#8: join()

Check out how to use join method in python.

#9: in and out

a = ‘My Own String’
b= ‘My’ in a
print(b)

Result : True

a = ‘My Own String’
b= ‘My’ not in a
print(b)

Result: False

#10: endswith()

a = ‘My Own String’
b= a.endswith(‘String’)
print(b)

Result: True

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.