String is nothing but collection of sequence of character,whichever enclosed in single quotes(‘ ‘),double quotes (“ “) and triple quotes (”’ ”’)
Python strings are immutable
String can be a cobination of numbers and alphabates
String can be a empty also ,that has 0
Example:
C
1
2
3
4
5
6
7
8
9
10
11
str1='I am single quote string'
str2="I am double quote string"
str3='''I am triple quote string'''
print(str1)
print(str2)
print(str3)
Output:
Python
1
2
3
4
5
Iam single quote string
Iam double quote string
Iam triple quote string
Create empty string:
str1=”
str2=””
str3=”””
print (str1)
print (str2)
print (str3)
Output:
Note: it will print blanck on command prompt
Escape Sequence:
We can use escape sequence in string for special cases
Examples:
Escape Sequence
Meaning of ES
\n
Newline – It will Prints a newline character
\t
Tab – It will Prints a tab character
\\
Backslash – It will Prints a backslash ( \ ) character
\’
Single quote – It will Prints a single quote
\”
Double quote – It Will Prints a double quote
Example:
Python
1
2
3
4
5
6
7
8
9
10
11
str1="I\'m a \"double quote string\" in double quotes"
str2="I\'m a \'single quote string\' in double quotes"
str3='d:\\user\\python'
print(str1)
print(str2)
print(str3)
Output:
Python
1
2
3
4
5
I'm a "double quote string" in double quotes
I'ma'single quote string'indouble quotes
d:\user\python
Access the individual characters in strings:
To Access the individual characters from string we can do it in two ways.They are
1.Forward Indexing
2.Backward Indexing
1.Forward Indexing:
P
Y
T
H
O
N
Index 0 1 2 3 4 5
As per the defination of string it will stored in sequence .We can access the individual characters in string by using the string variable and index.As per above schematic we need to generalize that forward index will start from “0” th index and end with “n-1” index.To use the index we can use square brackets “[]”
Below is the example to access the charatcters in the string
course=”python”
course[0]=’p’
course[1]=’y’
course[2]=’t’
course[3]=’h’
course[4]=’o’
course[5]=’n’
Below is the example is showing how to iterate the each character by using loops
Example:
Python
1
2
3
4
5
6
7
course="python"
length=len(course)
foriinrange(length):
print("course[%d]:%s"%(i,course[i]))
Output:
Python
1
2
3
4
5
6
7
8
9
10
11
course[0]:p
course[1]:y
course[2]:t
course[3]:h
course[4]:o
course[5]:n
2.Backward Indexing or negative indexing:
We can also use negative index in string to access the individual characters.Negative index will start from the last character ,it will start with “-1” and end with “-n”
P
Y
T
H
O
N
Negative Index -6 -5 -4 -3 -2 -1
Below is the example to access the charatcters in the string
course=”python”
course[-6]=’p’
course[-5]=’y’
course[-4]=’t’
course[-3]=’h’
course[-2]=’o’
course[-1]=’n’
Example:
Python
1
2
3
4
5
6
7
course="python"
length=len(course)
foriinrange(1,length+1):
print("course[%d]:%s"%(-i,course[-i]))
Output:
Python
1
2
3
4
5
6
7
8
9
10
11
course[-1]:n
course[-2]:o
course[-3]:h
course[-4]:t
course[-5]:y
course[-6]:p
String Slicing:
String slicing is used to get the set of characters at a time.To do this we can use the slicing operator(:)
Syntax:
string_name[startindex:endindex]
Return:
It will return the slice of characters from the start index to end index,the character end index is not be include in the slice.
If endindex is greater than the string length then it will give the complete string from start index to complete stringNote: Here index is optional
Example:
Python
1
2
3
4
5
6
7
string="python"
printstring[0:3]
printstring[2:5]
printstring[2:len(string)+5]
Output:
Python
1
2
3
4
5
pyt
tho
thon
Example:
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
string="python"
print(string[:])
print(string[1:])
print(string[3:])
print(string[:1])
print(string[:3])
print(string[0:3])
print(string[2:5])
print(string[2:len(string)+5])
print(string[:-1])
print(string[-1:])
print(string[-3:-1])
print(string[-1:-3])
Output:
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
python
ython
hon
p
pyt
pyt
tho
thon
pytho
n
ho
String Methods In Python:
By using string methods we can find the sub string in main string,convert string etc.
Syntax:
string_name.method_name(arg1,arg2…..argn)
Note:Here arguments are optinal
Methods for find and replace the strings:
Example:
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
string="i hi hello hai how are you?"
string1="hello"
print("The string is find at:",string.find("h"))
print("The string is rfind at:",string.rfind("h"))
print("The string is find at:",string.find("hey"))
print("The string is endswith:",string1.endswith("lo"))
print("The string is startwith:",string1.startswith("he"))
Output:
Python
1
2
3
4
5
6
7
8
9
The stringisfind at:2
The stringisrfind at:15
The stringisfind at:-1
The stringisendswith:True
The stringisstartwith:True
Strings converting Methods:
Example:
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string="i am leArNing pyThON"
string1="\n\tNewline\tstring"
print("String in lower cases:",string.lower())
print("String in upper cases:",string.upper())
print("String in first char upper case:",string.capitalize())