-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15. string-operations.py
More file actions
62 lines (27 loc) · 1.46 KB
/
Copy path15. string-operations.py
File metadata and controls
62 lines (27 loc) · 1.46 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# shortcut command for commenting -> Select and Ctrl + /
# string operations
name = "Sarbesh Mallick "
print (name)
print (name.upper())
print (name.lower())
print (name) # the original value is intact
# when we do any operations in string it dosen't impact the original value
# in python strings are Immutable
# find function
print (name.find('M')) # if that string exists then we will get the index of that thing
# find function returns index which is position
# if we search something that is not present we will get -1 as value which is invalid
# replace method
print (name.replace("Sarbesh Mallick" , "Muthu"))
print (name.replace("Mallick" , "Muthu")) # when we need to replace smthng partial
print (name.replace("S" , "D"))
# check function
print ('S' in name) # in is a keyword which checks presence and results comes in True or False
# in is a keyword in python dictionary, we cannot use in as a varibale name, in operator job is to search
# in is not a function but a operator.
# in() # ❌
# print() # function ✅
# A useful rule for your notes:
# Functions → generally called independently: print(), input(), int()
# Methods → called using . on an object: "hello".upper(), "hello".replace()
# So if your course asks "Is replace a function?", technically: str.replace() is a method, not a standalone function.