-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.strings.py
More file actions
133 lines (128 loc) · 4.53 KB
/
Copy path3.strings.py
File metadata and controls
133 lines (128 loc) · 4.53 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
food = 'samosa'
food[1]
'a'
# food[;2]
# SyntaxError: invalid syntax
# food[:2}
#SyntaxError: closing parenthesis '}' does not match opening parenthesis '['
food[:2]
'sa'
food[2:]
'mosa'
food[-3:]
'osa'
len(food)
6
myfoo='samosa, jalebi'
'jalebi' in myfoo
#True
'Jalebi' in myfoo
#False
'biryani' in myfoo
#False
'biryani' not in myfoo
#True
food[0] = 'x'
#Traceback (most recent call last):
# File "<pyshell#23>", line 1, in <module>
# food[0] = 'x'
#TypeError: 'str' object does not support item assignment
myfoo
'samosa, jalebi'
myfoo.replace('jalebi', 'biryani')
'samosa, biryani'
myfoo
'samosa, jalebi'
dir(myfoo)
#['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
myfoo.lower()
'samosa, jalebi'
myfoo.upper()
'SAMOSA, JALEBI'
myfoo.index('jalebi')
8
myfoo.index('biryani')
#Traceback (most recent call last):
#File "<pyshell#31>", line 1, in <module>
# myfoo.index('biryani')
# ValueError: substring not found
states=29
text=states+'states in India'
#Traceback (most recent call last):
# File "<pyshell#33>", line 1, in <module>
# text=states+'states in India'
# TypeError: unsupported operand type(s) for +: 'int' and 'str'
text=str(states)
newtext = states + 'states in India'
#Traceback (most recent call last):
# File "<pyshell#35>", line 1, in <module>
#newtext = states + 'states in India'
#TypeError: unsupported operand type(s) for +: 'int' and 'str'
newtext = str(states) + 'states in India'
newtext
'29states in India'
s='let\'s learn python'
s
"let's learn python"
# multiline string
dialog = '''kitne admi the?
kalia: 'sardar do!'''
dialog
"kitne admi the?\nkalia: 'sardar do!"
print(dialog)
# kitne admi the?
# kalia: 'sardar do!
# \n meaning spacer for new line
s='hey \tbro'
s
'hey \tbro'
print(s)
# hey bro
# \t meaning few spaces
# print combine string
first = 'test'
last = 'testing'
print(first + ' ' + last)
# test testing
# new way to combine using f string
#cprint(f'{first last}')
# SyntaxError: f-string: invalid syntax. Perhaps you forgot a comma?
print(f' {first}')
# SyntaxError: invalid syntax
print(f'{first}')
#test
print(f'{first} {last}')
# test testing
# 1. Create 3 variables to store street, city and country, now create address variable to
# store entire address. Use two ways of creating this variable, one using + operator and the other using f-string.
# Now Print the address in such a way that the street, city and country prints in a separate line
street = "13 patli gali"
city = "New Delhi"
country = "India"
address = street + '\n' + city + '\n' + country
print("Address using + operator:",address)
address = f'{street}\n{city}\n{country}'
print("Address using f-string:",address)
# 2. Create a variable to store the string "Earth revolves around the sun"
# 1. Print "revolves" using slice operator
# 2. Print "sun" using negative index
s='Earth revolves around the sun'
print(s[6:14])
print(s[-3:])
# 3. Create two variables to store how many fruits and vegetables you eat in a day.
# Now Print "I eat x veggies and y fruits daily" where x and y presents vegetables and fruits that you eat everyday.
# Use python f string for this.
num_fruits=10
num_veggies=5
print(f"I eat {num_veggies} veggies and {num_fruits} daily")
# 4. I have a string variable called s='maine 200 banana khaye'. This of course is a
# wrong statement, the correct statement is 'maine 10 samosa khaye'.
# Replace incorrect words in original strong with new ones and print the new string.
# Also try to do this in one line.
s='maine 200 banana khaye'
s=s.replace('banana','samosa')
s=s.replace('200','10')
print("Using two line replace:",s)
s='maine 200 banana khaye'
s=s.replace('banana','samosa').replace('200','10')
print("Using single line:",s)