×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Comprehensive Guide to Python String Methods

Last Updated : April 25, 2025

Python has many built-in string methods to work with Python strings. In this tutorial, we will learn about the most popular string methods in Python.

1. String strip() Method

This method will return a string in which all the characters specified have been stripped from the beginning and the end of the string.

Syntax

Here is the syntax of the String strip([chars]) method:

string.strip([chars])

Here,

  • chars - characters to be removed from the beginning or the end, by default space.

Example

The following is the example demonstrating the usage of the strip() method:

string="includehelp is a portal to learn concepts"
print(string)
print(string.strip('incspt'))  #removes characters I, n, c, s, p, t from beg and end
print(string.strip())    #removes spaces from beg and end

The output of the above code will be:

includehelp is a portal to learn concepts
ludehelp is a portal to learn conce
includehelp is a portal to learn concepts

2. String find() Method

This method returns the position of the first occurrence of a specified string between 'beg' and 'end', where beg is inclusive and end is exclusive. If the string is not found, it returns -1.

Syntax

Here is the syntax of the String find() method:

string.find(str, beg=0, end=len(string))

Here,

  • str - string to be searched
  • beg - starting index, by default 0
  • end - ending index, by default length of string

Example

The following is the example demonstrating the usage of the find() method:

string="includehelp is a portal to learn concepts"
print(string.find("include",0,6))  #return -1 as 6 is exclusive
print(string.find("include",0,7))
print(string.find("por"))

The output of the above code will be:

-1
0
17

3. String rfind() Method

This method returns the position of the last occurrence of the specified string. This function works similarly to find().

Syntax

Here is the syntax of the String rfind() method:

string.rfind(str, beg=0, end=len(string))

Here,

  • str - string to be searched
  • beg - starting index, by default 0
  • end - ending index, by default length of string

Example

The following is the example demonstrating the usage of the rfind() method:

string="includehelp is a portal to learn concepts"
print(string.rfind("include",0,6))  #return -1 as 6 is exclusive
print(string.rfind("include",0,7))
print(string.rfind("por"))

The output of the above code will be:

-1
0
17

4. String split() Method

As the name specifies, it splits the string from the specified delimiter str. This method returns a list.

Syntax

Here is the syntax of the String split(str, limit) method:

string.split(str, limit)

Here,

  • str - the delimiter on which to split the string. By default, it is a space.
  • limit - the maximum number of splits to perform. If not specified, all occurrences are split.

Example

The following is the example demonstrating the usage of the split() method:

string="includehelp is a portal to learn concepts"
l=string.split()
print(l)
m=string.split('o')
print(m)
n=string.split('o',1)
print(n)

The output of the above code will be:

['includehelp', 'is', 'a', 'portal', 'to', 'learn', 'concepts']
['includehelp is a p', 'rtal t', ' learn c', 'ncepts']
['includehelp is a p', 'rtal to learn concepts']

5. String lower() Method

This method returns a string with all the letters in lowercase.

Syntax

Here is the syntax of the String lower() method:

string.lower()

Example

The following is the example demonstrating the usage of the lower() method:

string = "IncludeHELP"
print(string.lower())  # output: "includehelp"

6. String upper() Method

This method returns a string with all letters in uppercase.

Syntax

Here is the syntax of the String upper() method:

string.upper()

Example

The following is the example demonstrating the usage of the upper() method:

string = "IncludeHELP"
print(string.upper())  # output: "INCLUDEHELP"

7. String title() Method

This method returns a string by converting it into title case i.e. the first letter of all the words will be uppercase.

Syntax

Here is the syntax of the String title() method:

string.title()

Example

The following is the example demonstrating the usage of the title() method:

string = "includehelp is a portal"
print(string.title())  # output: "Includehelp Is A Portal"

8. String capitalize() Method

This method returns a string with only the first letter of the first word as uppercase.

Syntax

Here is the syntax of the String capitalize() method:

string.capitalize()

Example

The following is the example demonstrating the usage of the capitalize() method:

string = "includehelp"
print(string.capitalize())  # output: "Includehelp"

9. String startswith() Method

This method returns true if the string starts with the specified string 'prefix', else returns false.

Syntax

Here is the syntax of the String startswith(prefix, beg=0, end=len(string)) method:

string.startswith(prefix, beg=0, end=len(string))

Here,

  • prefix - the string to be checked if it matches the beginning of the string.
  • beg - the starting index to begin checking, by default 0.
  • end - the ending index, optional, by default the length of the string.

10. String endswith() Method

This method returns true if the string ends with the specified string 'suffix', else returns false.

Syntax

Here is the syntax of the String endswith(suffix, beg=0, end=len(string)) method:

string.endswith(suffix, beg=0, end=len(string))

Here,

  • suffix - the string to be checked if it matches the end of the string.
  • beg - the starting index to begin checking, by default 0.
  • end - the ending index, optional, by default the length of the string.

Example

The following is the example demonstrating the usage of the startswith() and endswith() methods:

string="Includehelp is a portal to Learn Concepts"
print(string.lower())
print(string.upper())
print(string.title())
print(string.capitalize())
print(string.endswith('rn',0,32))
print(string.startswith('rt',19))

The output of the above code will be:

includehelp is a portal to learn concepts
INCLUDEHELP IS A PORTAL TO LEARN CONCEPTS
Includehelp Is A Portal To Learn Concepts
Includehelp is a portal to learn concepts
True
True

11. String len() Method

This function will return the length of the string str given as an argument.

Syntax

Here is the syntax of the String len() method:

len(string)

Example

The following is the example demonstrating the usage of the len() method:

string = "Includehelp"
print(len(string))  # output: 11

12. String lstrip() Method

This method will remove all the characters specified from the left of the string.

Syntax

Here is the syntax of the String lstrip([chars]) method:

string.lstrip([chars])

Here,

  • chars - optional parameter that specifies the characters to remove from the left end of the string. By default, it removes leading spaces.

13. String rstrip() Method

This method will remove all the characters specified from the right of the string.

Syntax

Here is the syntax of the String rstrip([chars]) method:

string.rstrip([chars])

Here,

  • chars - optional parameter that specifies the characters to remove from the right end of the string. By default, it removes trailing spaces.

Example

The following is the example demonstrating the usage of the lstrip() and rstrip() methods:

string="Includehelp is a portal to Learn Concepts"
print('Length of string is',len(string))
print(string.lstrip('Isctn'))
print(string.rstrip('Instpc'))

The output of the above code will be:

Length of string is 41
ludehelp is a portal to Learn Concepts
Includehelp is a portal to Learn Conce

14. String str.join() Method

This method will return a string, which is the concatenation of the strings in 'sequence' by the separator str.

Syntax

Here is the syntax of the String join(sequence) method:

string.join(sequence)

Here,

  • sequence - this is the sequence of elements (e.g., list, tuple) to be joined. The string that calls join() will act as a separator between the elements of the sequence.

Example

The following is the example demonstrating the usage of the join() method:

str=' '
l=('Include','help','is','a','portal','to','learn')
print(str.join(l))
str='_'
l=('Include','help')
print(str.join(l))

The output of the above code will be:

Include help is a portal to learn
Include_help

15. String isalpha() Method

This method returns true if the string only contains alphabets, else returns false.

Syntax

Here is the syntax of the String isalpha() method:

string.isalpha()

Here,

  • isalpha() - returns True if all characters in the string are alphabetic, otherwise returns False.

16. String isalnum() Method

This method returns true if the string is a combination of alphabets and numbers only, else returns false.

Syntax

Here is the syntax of the String isalnum() method:

string.isalnum()

Here,

  • isalnum() - returns True if all characters in the string are alphanumeric (consisting of only letters and numbers), otherwise returns False.

Example

The following is the example demonstrating the usage of the isalpha() and isalnum() methods:

string="Includehelp"
string1="Include help"
string2='123456'
string3='12345Hii'
print(string.isalpha())
print(string1.isalpha())  #It will return False as string1 contains a space
print(string2.isalnum())
print(string3.isalnum())

The output of the above code will be:

True
False
True
True

17. String count() Method

This method will count the occurrence of the substring between specified indices.

Syntax

Here is the syntax of the String count(str, beg=0, end=len(string)) method:

string.count(str, beg=0, end=len(string))

Here,

  • str - the substring to be counted in the string.
  • beg - the starting index, by default 0.
  • end - the ending index, by default the length of the string.

Example

The following is the example demonstrating the usage of the count() method:

string="Includehelp"
print(string.count("l"))
print(string.count("l",0,4))

The output of the above code will be:

2
1

Python String Methods Exercise

Select the correct option to complete each statement about Python string methods.

  1. The method str.upper() is used to:
  2. The method str.strip() is used to:
  3. The method str.split() is used to:
  4. The method str.replace() is used to:

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.