Python String methods help manipulate and analyze text data. These include lower(), upper(), replace(), strip(), split(), and join(). These allow you to clean, transform, and extract information from the text data effectively.

Table of contents

  1. Python String Methods
    1. find
    2. upper
    3. lower
    4. strip
    5. replace
    6. split
    7. join
    8. in
    9. not in
    10. endswith
Python string methods
Photo by Wojtek Paczeu015b on Pexels.com

Python String Methods

Here is a list of the Python string methods mentioned in the provided content:

  1. find: Returns the index of a substring within a string.
  2. upper: Converts a string to uppercase.
  3. lower: Converts a string to lowercase.
  4. strip: Removes specified characters from the beginning and end of a string.
  5. replace: Replaces a portion of a string with a new string.
  6. split: Splits a string into a list of substrings.
  7. join: Joins elements of a list into a single string.
  8. in: Checks if a character or substring is present in a string.
  9. not in: Checks if a character or substring is not present in a string.
  10. endswith: Checks if a string ends with a specified substring.

These methods can be helpful in various data analytics projects.

find

The Python string find function provides an index for substring.

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

Result: 3 (index)
Advertisements

upper

The upper function in Python converts string to upper case.

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

lower

The lower method in Python converts the string to lowercase.

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

strip

The strip function in Python strips a portion from the string.

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

replace

The Python replace and replace all methods are used to substitute an existing string with a 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)

split

The split function in Python divides strings into lists.

a = 'My Own String'
b= a.split()
print(b)
Result: ['My', 'Own', 'String']

join

Check out how to use the join method in Python.

in

The in and not in operators in Python are used to check if a character or string is present in another string.

a = 'My Own String'
b= 'My' in a
print(b)

Result : True

not in

a = 'My Own String'
b= 'My' not in a
print(b)
Result: False

endswith

The Python code checks if a string ends with a specific portion.

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