Python - String
Python - String
Table of Contents
1. Gentle reminder
2. What is a string?
2.1. Definition 1
2.2. Definition 2
3. Creating string
name1 = 'Daniel'
name2 = "Daniel"
name3 = '''Daniel'''
name4 = """Daniel"""
name1= 'Daniel'
name2 = "Prasad"
name3 = '''Mouli'''
name4 = """Veeru"""
output
Daniel name is created by using single quotes
Prasad name is created by using double quotes
Mouli name is created by using triple single quotes
Veeru name is created by using triple double quotes
Make a note
3.1. When should we go for triple single and triple double quotes?
✓ If you want to create multiple lines of string, then triple single or triple
double quotes are the best to use.
print(loc1)
print(loc2)
output
TCS company
White Field
Bangalore
TCS company
Bangalore
ITPL tech park
Diagram representation
print(wish[0])
print(wish[1])
Output
H
e
print(wish[0:7])
Output
Hello W
Output
H
e
l
l
o
W
o
r
l
d
4. Mutable
5. Immutable
name = "Daniel"
print(name)
print(name[0])
output
Daniel
D
name = "Daniel"
print(name)
print(name[0])
name[0]="X"
output
Daniel
D
TypeError: 'str' object does not support item assignment
a = "Python"
b = "Programming"
print(a+b)
output
PythonProgramming
course="Python"
print(course*3)
output
PythonPythonPython
10 | P a g e 10.Python - String
Data Science - Python String
course = "Python"
print(len(course))
Output
6
11 | P a g e 10.Python - String
Data Science - Python String
Definition 1
Definition 2
9.1. in operator
Program in operator
Name demo12.py
print('p' in 'python')
print('z' in 'python')
print('on' in 'python')
print('pa' in 'python')
output
True
False
True
False
12 | P a g e 10.Python - String
Data Science - Python String
output
True
13 | P a g e 10.Python - String
Data Science - Python String
print(dir(str))
output
14 | P a g e 10.Python - String
Data Science - Python String
Important methods
Important point
15 | P a g e 10.Python - String
Data Science - Python String
✓ upper()
✓ lower()
✓ strip()
✓ count(p)
✓ replace(p1, p2)
✓ split(p)
16 | P a g e 10.Python - String
Data Science - Python String
name = "daniel"
print("Before converting: ", name)
print("After converting: ", name.upper())
Output
Before converting: daniel
After converting: DANIEL
17 | P a g e 10.Python - String
Data Science - Python String
name = "DANIEL"
print("Before converting: ", name)
print("After converting: ", name.lower())
Output
Before converting: DANIEL
After converting: daniel
18 | P a g e 10.Python - String
Data Science - Python String
Make a note
✓ This method will not remove the spaces which are available middle of
string object.
Output
with spaces course length is: 18
after removing spaces, course length is: 6
19 | P a g e 10.Python - String
Data Science - Python String
output
2
0
20 | P a g e 10.Python - String
Data Science - Python String
print(s1)
print(s2)
output
Java programming language
Python programming language
21 | P a g e 10.Python - String
Data Science - Python String
print(s1)
print(s2)
print(id(s1))
print(id(s2))
output
Java programming language
Python programming language
49044256
48674600
String objects are immutable, Is replace(p1, p2) method will modify the string
objects?
22 | P a g e 10.Python - String
Data Science - Python String
Splitting of Strings:
print("Before splitting:", s)
print("After splitting: ", n)
output
23 | P a g e 10.Python - String
Data Science - Python String
print("Before splitting:", s)
print("After splitting: ", n)
output
24 | P a g e 10.Python - String