10 Must-read String Methods in Python

Here are top string methods in python helpful for data analytics projects.

String methods in python with examples

  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()

The “help” method provides details about methods.

a="my own string"
b=help(a.upper)
print(b)


Result:

Help on built-in function upper:

upper() method of builtins.str instance
    Return a copy of the string converted to uppercase.

None

** Process exited - Return Code: 0 **
Press Enter to exit terminal
Advertisements

2. find()

The “find” method provides index for a word.

a="my own string"
b=a.find('own')
print(b)

Result: 3 (index)

3. upper()

The “upper” method coverts lower case to upper.

a="my own string"
b=a.upper()
print(b
Result: MY OWN STRING (converts to upper case)

4. lower()

The “lower” method converts upper case to lower.

a="my OWN string"
b=a.lower()
print(b)
Result: my own string (converts to lower)

5. strip()

The “strip” method truncates a portion from the string.

a="my Own String"
b=a.strip('my')
print(b)
Resut: Own String (it strips only edges)

6. replace()

The “replace” method will replace with new string.

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()

The “split” method splits the string into a list.

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 & not in

The “in” and “not in” methods checks presence of a char or string.

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()

The “endswith” checks for the ending portion of the string.

a = 'My Own String'
b= a.endswith('String')
print(b)
Result: True
Related

Author: Srini

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