String in Python New
String in Python New
String is a sequence which is made up of one or more UNICODE characters. Here the character can be a
letter, digit, whitespace or any other symbol. A string can be created by enclosing one or more characters
in single, double or triple quote.
Each individual character in a string can be accessed using a technique called indexing. The index
specifies the character to be accessed in the string and is written in square brackets ([ ]). The index of
the first character (from left) in the string is 0 and the last character is n-1 where n is the length of the
string.
If we give index value out of this range then we get an IndexError. The index must be an integer
(positive, zero or negative).
String H e l l o W o r l d !
Positive Index 0 1 2 3 4 5 6 7 8 9 10 11
An inbuilt function len() in Python returns the length of the string that is passed as parameter. For
example, the length of string str1 = 'Hello World!' is 12.
String Operations
As we know that string is a sequence of characters. Python allows certain operations on string data type,
such as concatenation, repetition, membership and slicing. These operations are explained in the
following subsections with suitable examples.
Concatenation:
To concatenate means to join. Python allows us to join two strings using concatenation operator plus
which is denoted by symbol +.
Repetition:
Python allows us to repeat the given string using repetition operator which is denoted by symbol *.
Python has two membership operators 'in' and 'not in'. The 'in' operator takes two strings and
returns True if the first string appears as a substring in the second string, otherwise it returns False.
Slicing:
In Python, to access some part of a string or substring, we use a method called slicing.
This can be done by specifying an index range. Given a string str1, the slice operation str1[n:m]
returns the part of the string str1 starting from index n (inclusive) and ending at m (exclusive). In other
words, we can say that str1[n:m] returns all the characters starting from str1[n] till str1[m-1].
The numbers of characters in the substring will always be equal to difference of two indices m and
n, i.e., (m-n).
The slice operation can also take a third index that specifies the ‘step size’.
For example, str1[n:m:k], means every kth character has to be extracted from the string str1
starting from n and ending at m-1. By default, the step size is one.
>>> str1[0:10:2]
'HloWr'
>>> str1[0:10:3]
'HlWl'
'World'
We can access each character of a string or traverse a string using for loop and while loop.
In the above code, the loop starts from the first character of the string str1 and automatically ends
when the last character is accessed.
Here while loop runs till the condition index < len(str) is True, where index varies from 0 to
len(str1) -1.
String Methods and Built-in Functions:
Python has several built-in functions that allow us to work with strings.
Returns the string with all uppercase >>> str1 = 'hello WORLD!'
lower() >>> str1.lower()
letters converted to lowercase
'hello world!'
Returns the string with all lowercase >>> str1 = 'hello WORLD!'
upper() >>> str1.upper()
letters converted to uppercase
'HELLO WORLD!'
Returns number of times substring str >>> str1 = 'Hello World! Hello
Hello'
occurs in the given string.
>>> str1.count('Hello',12,25)
count(str, If we do not give start index and end
2>
start, end) index then searching starts from index >> str1.count('Hello')
0 and ends at length of the string 3