You are on page 1of 6

BASIC OPERATION ON STRING:

A Python String is a sequence of characters. Python Strings are immutable, it


means once we declare a string, we can’t modify it. Python provides a built-in
class “str” for handling text as the text is the most common form of data that a
Python program handles. Following are the common string operations that can
be performed in Python:
 Concatenation of two or more strings.
 Extracting or slicing partial strings from string values.
 Adding or removing spaces.
 Converting to lower or upper case.
 Formatting strings using string formatters.
 Finding and/or replacing a text in the given string with some other text.
And the list of operations is countless. Python provides several built-in
methods that let us to perform operations on a string in a flexible way.
Some of the basic operation on strings are:
1.len() 2.index() 3.count() 4.upper() 5.lower() 6.startswith() 7.endswith()
8.split()
1. len()
len() is a built-in function in python. You can use the len() to get the length of
the given string, array, list, tuple, dictionary, etc. You can use len function to
optimize the performance of the program. The number of elements stored in
the object is never calculated, so len helps provide the number of elements.It
returns the number of characters in a string, which includes punctuation,
space, and all type of special characters. It will return an integer value i.e. the
length of the given string.

Syntax:
len(value)
Example:
str= “Hello World”
print(len(str))
OUTPUT = 11
2. index()
Python string method index() determines if string str occurs in string or in a
substring of string if starting index beg and ending index end are given. This
method is same as find(), but raises an exception if sub is not found.
Syntax:
Index(‘value’)
Example:

str=“My name is Neha Maqsood”


print(str.index(‘a’))
OUTPUT:
4

3. count()
The count() is a built-in function in Python. It will return the total count of a
given element in a string. The counting begins from the start of the string till
the end. It is also possible to specify the start and end index from where you
want the search to begin.
The count() method will return an integer value, i.e., the count of the given
element from the given string. It returns a 0 if the value is not found in the
given string.
Syntax:
count(‘value’)
Example:
str= “Hello World”
print(str.count(‘l’))
OUTPUT= 3
4. upper()
Python String upper() is used to convert characters in given string into
uppercase. upper() method returns a new string with the characters of this
string converted to uppercase.
Syntax
upper()

5. lower()
Python String lower() is used to convert characters in given string into
lowercase. lower() method returns a new string with the characters of this
string converted to lowercase.

Syntax
lower()
6. startswith()
Python String.startswith() is used to check if this string starts with a specified
value. The startswith() method returns a boolean value of True if the starts
with specified value, or False if not.
Syntax
The syntax to call startswith() method on string x in Python is
x.startswith(‘value’)
7. endswith()
Python String endswith() method returns True if the string ends with the
specified value, or False if the string does not end with the specified value.
Syntax:
endswith(value)

8. split()
A split function in Python provides a list of words in each line or a
string. Such strings are always separated by a delimiter string. It
provides one or more substrings from the main strings. The syntax of
the split is composed of a function with two parameters
termed separator and max with a return value.
The syntax of the split function is provided below: –
Syntax:
split (separator, max)

The following parameters of the function are described as follows:-


 Separator: A separator is defined to identify the delimiter. With
the use of a separator, the split function splits the main string
into multiple substrings.
 Maxsplit parameter: This is a number that is passed into the
function to split the string basis into the maximum number of
occurrences. It specifies how many splits to do. Default value is
-1 which means all occurrence.
 Return: The split function returns back to the list of strings after
breaking or splitting the main string.

You might also like