-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3String.py
57 lines (49 loc) · 1.94 KB
/
3String.py
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
56
57
"""
String is a big term to discuss.
In python a string is kept in either single quote or double quote.
"""
aString = "This is a string"
bString = 'This is also a string'
# Multiline string can be assigned within 3 single/ double quotation.
multiLin = """ Hello There,
This is a multiline string.
Python is amazing.
"""
#-_-_-_-_-_-_-| Sting Length |-_-_-_-_-_-_-
# len()
a = len(aString) # this will show the length of the string
#-_-_-_-_-_-_-| Check string |-_-_-_-_-_-_-
# in & not in
check1 = 'This' in bString # this will return true if 'This' exists in bString
check2 = 'Now' not in aString # this will return true if 'Now' does not exist in aString
#-_-_-_-_-_-_-| Slicing |-_-_-_-_-_-_-
# Slicing in string is like List.
# You can easily slice string with index number.
slc1 = multiLin[0: 4]
slc2 = multiLin[-1: -8]
slc3 = multiLin[:6]
#-_-_-_-_-_-_-|Modifying string|-_-_-_-_-_-_-
# _.upper() & _.lower()
word1 = "sciENce"
upperCase = word1.upper() # This will make all the latter in the variable Uppercase
lowerCase = word1.lower() # This will make all the latter in the variable Lowercase
# _.replace(a, b) ---- here a will be replaced by b
location = "England"
location = location.replace("E", 'P')
# _.split(split_point)
sentence1 = 'This is so sweet.'
spl1 = sentence1.split('s')
# _.format(var1, var2, var3)
age = 10
name2 = 'Tom'
color1 = 'Green'
print("this is {1}. He is {0} years old.".format(age, name2)) # the variable in the format() function has index number. You can assign them using that index
print("this is {}. He is {} years old. His favourite color is {}".format(name2, age, color1)) # you also can use without indexing. But in that case you must put them in the order you want them to be.
#-_-_-_-_-_-_-|Scape character|-_-_-_-_-_-_-
# you can not use double quote in double quote. that's when scape character comes to help you
print("I love \"Rocket Science\" ")
"""
\n to create new line
\t to open a new tab
\\ to add a \ in the line
"""