Table of contents

String Methods

 In addition to Indexing and Slicing Operations, we can perform various other operations on the string object by using pre-defined functions present in the string.

The pre-defined functions present in the string  are

capitalize():

capitalize()Converts the string's first character to a capital letter.  

# Capitalize a = "name" print(a.capitalize())

Upper is the pre-defined function. It can be used to convert enteir string into uppercase

# upper name = "cedlearn" print(name.upper())

Lower is the pre-defined function. It can be used to convert an entire string into lowercase. 

# lower name = "cedlearn" print(name.lower())

swapcase is the predefined function. It can be used to convert lowercase to uppercase and uppercase to lowercase.

# swapcase str = "HELLO python" print(str.swapcase())

casefold is the predefined function. It can be used to convert uppercase to lowercase.

# casefold str = "KIRAN MARAAPU" print(str.casefold())

Strim is the predefined function. It can be used to remove the unwanted spaces between the string. It will take only one space.

# Strip str = "Hello Python" print(str.Strip())

Rstrip is a predefined function. It can be used to remove the right side space of the string

# rstrip str = " Hello " print(str.rstrip())

Lstrip is a pre-defined function. It can be used to remove the left side space of the string.

Index is a predefined function. It can be used to find specified character index positions. If the character is not there, it will show an error. If the multiple same character, it is always taken as the first character index. 

## index str = " Hello Python" srt.index("e")
## error ## index str = " Hello Python" srt.index("M") # error
### str = "Hello World" str.index("l") # index 2

Find is the predefined function. It can be used to find the specified character index position. If the character is not there it won't give any error. It will give the default values as -1. 

## str = "Hello Cedlearn" str.find("o") 4
## str = "Hello" str.find('k') # -1

count()

count() function in Python is used to count the occurrences of a substring within a given string.

  • substring: The string to be counted within the main string.
  • start (optional): The starting index from where to begin counting.
  • end (optional): The ending index where counting stops (not included).

Syntax : string.count(substring, start, end)

text = "hello world, hello universe" count_hello = text.count("hello") print(count_hello)
text = "hello world, hello universe" count_hello = text.count("l") print(count_hello)

Len() is the pre-defined function that can be used to find the length of the string.

str = "Hello Python" print(len(str))

isalnum() is a predefined function. If the string contains the alphabet or numbers, or both, then it will give True. Otherwise, it shows False. 

var = "hello555" var.isalnum() # True
var = "555" var.isalnum() # True
var = "Kiran" var.isalnum() # True
var = "Kiran@" var.isalnum() # False
var = "123@" var.isalnum() # False

isnum() is the predefined function. If the string contains a number, then only True, otherwise False.

# var = "123" print(var.isnum()) # True
var = "as123" print(var.isnum()) # False

isalpha is the predefined function. If the string contains the alphabet then onely it is True, otherwise False.

var = "hello" print(var.aialpha())