-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrings.py
56 lines (44 loc) · 1.29 KB
/
Strings.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
str1 = "This is a string. we are creating it in a python."
str2 = 'this is a string.\nwritten in single quotes' # \n is used to go in next line
str3 = """This is a string.\twritten in tripple quotes""" #\t is used to tab
print(str1)
print(str2)
print(str3)
#concatenation
str1 = "Mushraf"
str2 = "ali"
print(str1 + str2)
#length of string
str1 = "Mushraf"
len1 = len(str1)
print(len1)
str2 = "ali"
len2 = len(str2)
print(len2)
MyName = str1 + " " + str2
print(MyName) # or print(str1 + str2)
print(len(MyName))
#indexing
str = "Mushraf Ali"
ch = str[2]
print(ch) #print(str[2])
#slicing
str = "Mushraf Ali" #0to11
print(str[0:4]) #print(str[:4])
print(str[4:len(str)]) #print(str[4:])
str = "Apple" #-1to-5
print(str[-3:-1])
#String Functions
str = "i am studying python from ApnaCollege."
print(str.endswith("ege.")) #returs true if string ends with substr
print(str.capitalize()) #their is no change in old string
print(str)
#to make change
str = str.capitalize() #capitalizes 1st char
print(str)
print(str.replace("o", "a")) #replaces all occurrences of old
print(str.replace("python", "javascript"))
print(str.find("o")) #returns 1st index of 1st occurrer
print(str.find("python"))
print(str.find("Q")) #if their is no such letter it will return -1
print(str.count("o")) #counts the occurrences of substr