String
In [ ]: String is a collection of characters enclosed by single quotes, double quotes or
Triple Quotes:
- Sinlge Triple Quotes
- Double Triple Quotes
characters >> a-z,A-Z, 0-9, special chars(!,@,$,%,^<>?), space
x = "Python" # Double quotes
y = 'python' # Single quotes
z = """Python and Data science""" # Triple double quotes
w = '''Python and Data science''' # Triple single quotes
Access String Characters
In [ ]: 1. string indexing
2. string slicing
1. String Indexing
In [ ]: Indexing is used to access individual elements of a sequence.
To access item from string:
string[index]
1. Positive Indexing:
Left to right
string[0]
string[1] ...
2. Negative Indexing:
Python allows negative indices to access elements from the end of the sequen
right to left
string[-1]
string[-2] ...
string = p y t h o n
pos index = 0 1 2 3 4 5
neg index = -6 -5 -4 -3 -2 -1
2. String Slicing
In [ ]: Used to access one or more than one characters of String
Syntax:
1. without step size
string[start_index(include) : end_index(exclude)]
default start_index ==> 0
default end index ==> len(string)
ex. print(string[1:4])
print(string[-5:-2])
print(string[1:-2])
print(string[-5:4])
2. with step size:
string[start_index(include) : end_index(exclude) : step_size]
2.1 Positive step size:(Forward Step Size)
string[0:len(string):1]
string[1:5:1]
2.2 Reverse Step Size:(Reverse Step size)
string[::-1]
string[6:1:-1]
String Concatenation
In [ ]: - Joining two or more strings together.
- Use the + operator to concatenate strings
Example:
s1 = "P" + "D" + "S"
output >> "PDS"
Type Casting
In [ ]: integer to string:
str(int)
str(1456)
float to string:
str(float)
str(45.678)
string to integer:
int(str)
int('4567')
string to float:
float('34.56')
length function
In [ ]: len() is a built-in function in Python
- The len() function returns the number of characters in a string
- This includes letters, numbers, spaces, and special characters
Example:
len("Python") >> 6
len("Data Science") >> 12
- We cannot use len() directly on integer or float values because they are n
input function
In [ ]: input() is a built-in function in Python
- Used to take input from the user.
- Returns the input as a string.
- Syntax:
x = input("Enter something: ")
- Example:
x = input("Enter the value of x :") # x = "478"
eval() function
In [ ]: eval() is a built-in function in Python
- Evaluates a string expression as Python code.
- Can be used to convert string input into a number
- Example:
input:
x = eval("345")
output:
x = 345 # int datatype
input:
x = eval("3.45")
output:
x = 3.45 # float datatype
In [ ]:
Length Function
In [ ]: length is a built-in function python
We cannot use len() directly on integer or float values because they are not ite
In [2]: s1 = "python"
len(s1) #
Out[2]: 6
In [3]: x = 35792
len(x)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[3], line 2
1 x = 35792
----> 2 len(x)
TypeError: object of type 'int' has no len()
In [4]: x = 35.792
len(x)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[4], line 2
1 x = 35.792
----> 2 len(x)
TypeError: object of type 'float' has no len()
In [5]: s1 = "data science" # 4 + 1 + 7
len(s1)
Out[5]: 12
In [6]: s2 = " python "
len(s2)
Out[6]: 8
In [7]: s2 = "python @3.13"
len(s2) # 12
Out[7]: 12
In [8]: s2 = "python @3.13 "
len(s2) # 13
Out[8]: 13
In [9]: s4 = "87356"
len(s4)
Out[9]: 5
In [11]: s4 = '87356'
len(s4)
Out[11]: 5
In [12]: print("machine learning")
print('deep learning')
machine learning
deep learning
In [13]: s5 = "D"
len(s5)
Out[13]: 1
Access String items(chars)
1. String Indexing
In [ ]: Indexing always starts from 0
string = p y t h o n
pos index >> 0 1 2 3 4 5
neg index >> -6 -5 -4 -3 -2 -1
In [14]: string = "Python"
string[0] # "P"
Out[14]: 'P'
In [16]: string[1]
Out[16]: 'y'
In [17]: string[2]
Out[17]: 't'
In [18]: string[3]
Out[18]: 'h'
In [19]: string[4]
Out[19]: 'o'
In [21]: string[5]
Out[21]: 'n'
In [22]: string[6]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[22], line 1
----> 1 string[6]
IndexError: string index out of range
In [23]: string = "Python"
string[0]
string[1]
Out[23]: 'y'
In [25]: string = "Python"
string[0]
string[1]
string[2]
string[3]
Out[25]: 'h'
In [27]: string = "Python"
print(string[0])
print(string[1])
print(string[2])
print(string[3])
print(string[4])
print(string[5])
P
y
t
h
o
n
In [30]: string = "Python"
print(string[0])
print(string[1])
print(string[2])
print(string[3])
print(string[4])
print(string[5])
print(string[6])
print(string[7])
P
y
t
h
o
n
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[30], line 8
6 print(string[4])
7 print(string[5])
----> 8 print(string[6])
9 print(string[7])
IndexError: string index out of range
In [33]: string1 = "data science"
string1[0]
string1[4]
Out[33]: ' '
In [36]: string1 = "python"
length = len(string)
print(length)
In [38]: string1 = "python"
length = len(string)
print("Length of string is :",length)
string1[length - 1] # string1[6-1]
Length of string is : 6
Out[38]: 'n'
Negative Indexing
In [40]: string = "python"
string[-1]
Out[40]: 'n'
In [42]: string = "python"
string[-2]
Out[42]: 'o'
In [44]: string = "python"
string[-3]
Out[44]: 'h'
In [46]: string = "python"
string[-4]
Out[46]: 't'
In [48]: string = "python"
string[-5]
Out[48]: 'y'
In [50]: string = "python"
string[-6]
Out[50]: 'p'
In [52]: string = "python"
string[-7]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[52], line 2
1 string = "python"
----> 2 string[-7]
IndexError: string index out of range
In [ ]:
In [ ]: string = p y t h o n
pos index >> 0 1 2 3 4 5
neg index >> -6 -5 -4 -3 -2 -1
In [54]: string = "python"
print(string[2]) # t
print(string[-4]) # t
t
t
In [56]: string = "python"
print(string[4]) # o
print(string[-2]) #
o
o
In [58]: string = "python"
print(string[1]) # y
print(string[-5]) # y
y
y
In [61]: x = 5 + 6
x
Out[61]: 11
In [62]: x = 5 + 6.6
x
Out[62]: 11.6
String concatenation
In [64]: x = "D" + "S"
x
Out[64]: 'DS'
In [66]: x = "D" + 8
x
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[66], line 1
----> 1 x = "D" + 8
2 x
TypeError: can only concatenate str (not "int") to str
In [68]: s1 = "D" + "S" + "P"
s1
Out[68]: 'DSP'
In [70]: s1 = "P" + "D" + "S"
s1
Out[70]: 'PDS'
In [72]: s2 = " "
type(s2)
Out[72]: str
In [74]: s2 = " "
len(s2)
Out[74]: 1
In [76]: s1 = "P" + " " + "D" + "S"
s1
Out[76]: 'P DS'
In [77]: s1 = "Python" + " " + "Data" + "Science"
s1 # Python DataScience
Out[77]: 'Python DataScience'
In [78]: s2 = "Python"
s2[2] + s2[3] # "t" + "h" >> "th"
Out[78]: 'th'
2. String Slicing
In [ ]: Used to access one or more than one characters of string
string[start_index(include) : end_index(exclude)]
start_index >> Default value is 0
end_index >> len(string)
string[start_index(include) : end_index(exclude) : step_size]
start_index >> Default value is 0
end_index >> Default value is len(string)
step_size >> Default value is 1
In [ ]:
In [80]: string = "python"
string[1:4] # 1,2,3 >> 'yth'
Out[80]: 'yth'
In [82]: string = "science"
string[2:6] # 2,3,4,5 >> ienc
Out[82]: 'ienc'
In [83]: string = "science"
string[2:3] # 2 > i
Out[83]: 'i'
In [85]: string = "science"
string[2:2] # ''
Out[85]: ''
In [87]: string = "science"
string[4:2] # ''
Out[87]: ''
In [89]: string = "science"
string[0:3] # 0,1,2
Out[89]: 'sci'
In [90]: string = "science"
string[:3] # string [0:3]
Out[90]: 'sci'
In [91]: string = "science"
string[:5] # string [0:5]
Out[91]: 'scien'
In [97]: data_science = """Data science is an interdisciplinary field that combines princ
programming, and artificial intelligence to analyze and extract insights from la
print(data_science)
Data science is an interdisciplinary field that combines principles from mathemat
ics, statistics,
programming, and artificial intelligence to analyze and extract insights from lar
ge amounts of data.
In [98]: len(data_science)
Out[98]: 199
In [100… data_science = """Data science is an interdisciplinary field that combines princ
programming, and artificial intelligence to analyze and extract insights from la
print(data_science)
print(len(data_science))
print(data_science[:30])
Data science is an interdisciplinary field that combines principles from mathemat
ics, statistics,
programming, and artificial intelligence to analyze and extract insights from lar
ge amounts of data.
200
Data science is an interdiscip
In [101… data_science = """Data science is an interdisciplinary field that combines princ
programming, and artificial intelligence to analyze and extract insights from la
print(data_science)
print(len(data_science))
print(data_science[0:30])
Data science is an interdisciplinary field that combines principles from mathemat
ics, statistics,
programming, and artificial intelligence to analyze and extract insights from lar
ge amounts of data.
200
Data science is an interdiscip
In [103… string = "science"
string[3:6]
Out[103… 'enc'
In [105… string = "science"
string[3:7]
Out[105… 'ence'
In [108… string = "science"
string[3:200] # 3,4,5,6
Out[108… 'ence'
In [110… string = "science"
string[:]
Out[110… 'science'
In [112… string = "science"
string[:3]
Out[112… 'sci'
In [113… string = "science"
string[3:7] #
Out[113… 'ence'
In [115… string = "science"
string[3:]
Out[115… 'ence'
In [117… string = "science"
string[:]
Out[117… 'science'
In [ ]: string = p y t h o n
pos index >> 0 1 2 3 4 5
neg index >> -6 -5 -4 -3 -2 -1
In [119… string = "python"
print(string[1])
print(string[-5])
y
y
In [120… string = "python"
print(string[-5 : -2]) # -5, -4, -3 # yth
yth
In [122… string = "python"
print(string[1:4]) # 1,2,3
yth
In [ ]: string = p y t h o n
pos index >> 0 1 2 3 4 5
neg index >> -6 -5 -4 -3 -2 -1
In [126… string = "python"
print(string[2:5])
print(string[-4:-1])
print(string[2:-1])
print(string[-4:5])
tho
tho
tho
tho
In [128… string = "data science"
string[-5:-1] # -5,-4,-3,-2
Out[128… 'ienc'
In [130… string = "data science"
string[-5:]
Out[130… 'ience'
Last 30 characters of string
In [132… data_science = """Data science is an interdisciplinary field that combines princ
programming, and artificial intelligence to analyze and extract insights from la
print(data_science)
print(data_science[-30:])
Data science is an interdisciplinary field that combines principles from mathemat
ics, statistics,
programming, and artificial intelligence to analyze and extract insights from lar
ge amounts of data.
s from large amounts of data.
First 30 Characters of string
In [133… data_science = """Data science is an interdisciplinary field that combines princ
programming, and artificial intelligence to analyze and extract insights from la
print(data_science)
print(data_science[:30])
Data science is an interdisciplinary field that combines principles from mathemat
ics, statistics,
programming, and artificial intelligence to analyze and extract insights from lar
ge amounts of data.
Data science is an interdiscip
In [135… string = "science"
string[2:6] # 2,3,4,5
Out[135… 'ienc'
In [137… string = "science"
string[2:6:1] # 2,3,4,5
Out[137… 'ienc'
In [139… string = "science"
string[1:6:1] # 1,2,3,4,5 > cienc
Out[139… 'cienc'
In [141… string = "science"
string[1:6:2] # 1,2,3,4,5 > 1,3,5
Out[141… 'cec'
In [142… string = "DataScience"
string[0:8:1]
Out[142… 'DataScie'
In [144… string = "DataScience"
string[:8]
Out[144… 'DataScie'
In [146… string = "DataScience"
string[0:8:2] # DtSi
Out[146… 'DtSi'
In [149… data_science = """Data science is an interdisciplinary field that combines princ
programming, and artificial intelligence to analyze and extract insights from la
data_science[10:50:2]
Out[149… 'c sa nedsilnr il htc'
In [151… data_science = """Data science is an interdisciplinary field that combines princ
programming, and artificial intelligence to analyze and extract insights from la
data_science[0:50:3]
Out[151… 'Dacn tdcla e ac'
In [152… string = "science"
string[::1]
Out[152… 'science'
In [154… string = "science"
string[::]
Out[154… 'science'
In [156… string = "science"
string[:]
Out[156… 'science'
Negative Step Size
In [160… string = "science"
string[::1]
Out[160… 'science'
reverse a string
In [159… string = "science"
string[::-1] # Right to Left
Out[159… 'ecneics'
In [162… string = "Pyhton"
string[::-1] # Right to Left
Out[162… 'nothyP'
In [164… string = "science"
string[2 : 5]
Out[164… 'ien'
In [166… string = "science"
string[5 : 2]
Out[166… ''
In [168… string = "science"
string[5 : 2: 1]
Out[168… ''
In [170… string = "science"
string[5 : 2: -1] # 5,4,3
Out[170… 'cne'
In [172… string = "science"
string[5 : 0: -1] # 5,4,3,2,1
Out[172… 'cneic'
In [177… string = "science"
string[5 : 0: -2] # 5,4,3,2,1
Out[177… 'cec'
Type Casting
In [ ]: int to float
float to int
int to complex
float to complex
1. string to int
In [179… x = "5613"
int(x)
Out[179… 5613
In [181… x = "5611234567893"
int(x)
Out[181… 5611234567893
In [183… x = "561123.4567893"
int(x)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[183], line 2
1 x = "561123.4567893"
----> 2 int(x)
ValueError: invalid literal for int() with base 10: '561123.4567893'
In [ ]: x = "56 19"
int(x)
In [184… x = 56 19
Cell In[184], line 1
x = 56 19
^
SyntaxError: invalid syntax
In [185… x = "56 19"
int(x)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[185], line 2
1 x = "56 19"
----> 2 int(x)
ValueError: invalid literal for int() with base 10: '56 19'
In [186… x = "56.19"
int(x)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[186], line 2
1 x = "56.19"
----> 2 int(x)
ValueError: invalid literal for int() with base 10: '56.19'
In [189… x = " 5619 "
int(x)
Out[189… 5619
In [190… x = "5619"
int(x)
Out[190… 5619
string to float
In [192… x = "56.19"
float(x)
Out[192… 56.19
In [195… x = "56.19"
float(x)
x
Out[195… '56.19'
In [198… x = "56.19"
x = float(x)
x
Out[198… 56.19
In [199… x = " 56.19 "
x = float(x)
x
Out[199… 56.19
int to str
In [202… num = 456
s1 = str(num)
print(s1, type(s1))
456 <class 'str'>
In [204… num = 456
print(num, type(num))
s1 = str(num)
print(s1, type(s1))
456 <class 'int'>
456 <class 'str'>
In [206… x = 782
print(x, type(x))
x = str(x)
print(x, type(x))
782 <class 'int'>
782 <class 'str'>
In [207… x = 782
print(x, type(x))
str(x)
print(x, type(x))
782 <class 'int'>
782 <class 'int'>
In [209… x = "3.1467"
int(x)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[209], line 2
1 x = "3.1467"
----> 2 int(x)
ValueError: invalid literal for int() with base 10: '3.1467'
In [211… x = "3.1467"
x = float(x)
x
Out[211… 3.1467
In [212… x = "3.1467"
x = float(x)
x = int(x)
print(x, type(x))
3 <class 'int'>
float to str
In [214… x = 67.345
str(x)
Out[214… '67.345'
User input
In [215… input()
Out[215… '457'
In [216… a = input() # a = "25.98"
print(a, type(a))
25.98 <class 'str'>
In [217… a = float(input()) # a = float("25.98")
print(a, type(a))
25.98 <class 'float'>
In [218… a = int(input()) # a = int("524")
print(a, type(a))
524 <class 'int'>
In [219… a = int(input("Enter the value of a: "))
print(a, type(a))
986 <class 'int'>
In [221… a = int(input("Enter the value of a: ")) # a = int("3.15")
print(a, type(a))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[221], line 1
----> 1 a = int(input("Enter the value of a: ")) # a = int("3.15")
2 print(a, type(a))
ValueError: invalid literal for int() with base 10: '3.15'
In [223… a = int("3.15")
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[223], line 1
----> 1 a = int("3.15")
ValueError: invalid literal for int() with base 10: '3.15'
Eval Function
In [224… float("4.578")
Out[224… 4.578
In [226… int("4578")
Out[226… 4578
In [228… float("4578")
Out[228… 4578.0
In [230… int("45.78")
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[230], line 1
----> 1 int("45.78")
ValueError: invalid literal for int() with base 10: '45.78'
In [231… eval("45.78")
Out[231… 45.78
In [233… eval("4578")
Out[233… 4578
In [235… x = eval("45.78")
print(x,type(x))
45.78 <class 'float'>
In [236… x = eval("458")
print(x,type(x))
458 <class 'int'>
In [238… a = eval(input("Enter the value of a: ")) # a = int("3.15")
print(a, type(a))
67.92 <class 'float'>
In [ ]:
In [239… x = 5678
float(x)
Out[239… 5678.0
In [241… x = "5678"
float(x)
Out[241… 5678.0
In [243… x = "567.8"
float(x)
Out[243… 567.8
In [245… s1 = "python"
s1
Out[245… 'python'
In [247… s1 = "python"
print(s1)
python
In [249… x = "4678"
x
Out[249… '4678'
In [250… x = "4678"
print(x)
4678
In [251… x = input("enter value of x : ") # Default output of input function will be in s
x
Out[251… '68'
In [253… int('68')
Out[253… 68
In [254… x = input("enter value of x : ") # Default output of input function will be in s
print(x, type(x)) # 45 , str
x = int(x) # int('45')
print(x, type(x)) # 45 , int
45 <class 'str'>
45 <class 'int'>
In [255… x = int(input("enter value of x : ")) # Default output of input function will be
print(x, type(x)) # 45 , int
45 <class 'int'>