Negative indexing
Example:
n=”computer”
Positive index
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
c |
O |
m |
p |
u |
t |
e |
r |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Negative index
n=”computer”
print(n)
print(n[-0])
print(n[-2])
print(n[-4])
print(n[-5])
print(n[-7])
output:
computer
c
e
u
p
o
Example:
n1=”catalyst”
Positive index
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
c |
A |
t |
a |
l |
y |
s |
t |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Negative index
n1=’catalyst’
print(n1)
print(n1[-0])
print(n1[-2])
print(n1[-3])
print(n1[-6])
print(n1[-7])
catalyst
c
s
y
t
a
Python String functions and Methods
Python offers many built in functions and methods for string manipulation.
capitalize()
This function helps us to capitalize the first character of the string. And rest of the characters are converted to lower case.
syntax:
string.capitalize()
Example:
n="hello" print(n.capitalize()) n="HELLO" print(n.capitalize()) n="hello how are you" print(n.capitalize()) n="HELLO HOW ARE YOU" print(n.capitalize())
Output:
Hello
Hello
Hello how are you
Hello how are you
>>>
isalpha()
This function helps us to check whether all the character of a string are alphabets or not.
syntax:
string.isalpha()
Example:
n="hello" print(n.isalpha()) n="123" print(n.isalpha()) n="hello123" print(n.isalpha())
Output:
True
False
False
>>>
islower()
This function helps us to check whether all the character of a string are lower case alphabets or not.
syntax:
string.islower()
Example:
n="hello" print(n.islower()) n="123" print(n.islower()) n="hello123" print(n.islower()) n="Hello" print(n.islower())
Output:
True
False
True
False
>>>
isupper()
This function helps us to check whether all the character of a string are upper case alphabets or not.
syntax:
string.isupper()
Example:
n="HELLO" print(n.isupper()) n="123" print(n.isupper()) n="HELLO123" print(n.isupper()) n="Hello" print(n.isupper())
Output:
True
False
True
False
>>>
isdigit()
This function helps us to check whether all the character of a string are digits or not.
syntax:
string.isdigit()
Example:
n="HELLO" print(n.isdigit()) n="123" print(n.isdigit()) n="123HELLO123" print(n.isdigit()) n="Hello" print(n.isdigit())
Output:
False
True
False
False
>>>
isalnum()
This function helps us to check whether all the character of a string are alphabets or numbers or not.
syntax:
string.isalnum()
Example:
n="HELLO" print(n.isalnum()) n="123" print(n.isalnum()) n="123HELLO123" print(n.isalnum()) n="Hello" print(n.isalnum()) n="He#$%" print(n.isalnum())
Output:
True
True
True
True
False
>>>
isspace()
This function helps us to check whether all the character of a string are space or not.
syntax:
string.isspace()
example:
n=" " print(n.isspace()) n=' ' print(n.isspace()) n="hello" print(n.isspace())
Output:
True
True
False
>>>
lower()
This function helps us to convert all the characters of the string to lower case i.e. all upper case alphabets are converted to lower case rest remain as it is.
syntax:
string.lower()
Example:
n="Hello" print(n.lower()) n="HELLO" print(n.lower()) n="Hello How ARE You" print(n.lower())
Output:
hello
hello
hello how are you
>>>
upper()
This function helps us to convert all the characters of the string to upper case i.e. all lower case alphabets are converted to upper case rest remain as it is.
syntax:
string.upper()
Example:
n="Hello" print(n.upper()) n="HELLO" print(n.upper()) n="Hello How ARE You" print(n.upper())
Output:
HELLO
HELLO
HELLO HOW ARE YOU
>>>
find()
This function gives the position of the substring within the string.
returns -1 if substring is not present. (counting starts from 0)
syntax:
string.find(substring,start,end)
Example:
n="Hello" print(n.find('e')) n="HELLO" print(n.find('E')) n="hello" print(n.find('E')) n="hello how are you" print(n.find('e')) #start searching from 2nd position print(n.find('e',2)) #start searching from 2nd position and end searching at 10 position print(n.find('e',2,10))
Output:
1
1
-1
1
12
-1
>>>