Here are seven Python string manipulation methods (functions). Those are Find, Join, Validation, Change case, Split, Replace, and Strip. Note: In Python, there is no CHAR data type. So only strings you need to use.

Python string functions

Table of Contents

  1. Python string functions
    1. 1. Python string find
    2. 2. Python validate a string
    3. 3. Python string concatenation
    4. 4. Python change case of a string
    5. 5. Python string split
    6. 6. Python replace string
    7. 7. Python stripe string

Python string functions

Here are the list of Python string functions demonstrated with examples.

1. Python string find

Neat function to find specific string if it is present or not.

text = 'This is text'
nums = '123456'

# finding text
a= text.find('is')
b= text.find('your')
print(a)
print(b)

Output

2 
-1

** Process exited - Return Code: 0 **

2. Python validate a string

Helpful to do validation checks on the strings.

text = 'This is text'
nums = '123456'
# Validation checks
print(text.isalpha())
print(text.isdigit())
print(nums.isdigit())

Output

False 
False
True

** Process exited - Return Code: 0 **

3. Python string concatenation

Useful to concatenate strings with or without separator.

text = 'This is text'
nums = '123456'
a=''.join((text,nums))
b=' '.join((text,nums))
print(a)
print(b)

Output

This is text123456 
This is text 123456


** Process exited - Return Code: 0 **

4. Python change case of a string

Useful to convert case of the string.

text = 'This is text'
# case changing
print(text.upper())
print(text.lower())

Output

THIS IS TEXT 
this is text

** Process exited - Return Code: 0 **

5. Python string split

Effective to Split string into words.

# splitting string
text = 'This is text'
a=text.split(' ')
print(a)

Output

['This', 'is', 'text'] 

** Process exited - Return Code: 0 **

6. Python replace string

Handy method to replace part of the string with the other one.

text = 'This is text'
# substitution
a=text.replace('is','was')
print(a)

Output

Thiswas text 

** Process exited - Return Code: 0 **

7. Python stripe string

Advantageous to remove space in trailing and leading edges and both.

text1 = 'This is text '
a=text1.rstrip() # remove trailing space

text2 = ' This is text'
b=text2.lstrip() # remove leading space

text3 = ' This is text '
c=text3.strip() # remove trailing and leading spaces

print(a)
print(b)
print(c)

Output

This is text 
This is text
This is text

** Process exited - Return Code: 0 **