|
| 1 | +### Aim |
| 2 | + |
| 3 | +In this experiment, user will learn the concepts of string in Python programming language. |
| 4 | +To implement the operations that can be performed in a string. |
| 5 | + |
| 6 | +### Theory |
| 7 | + |
| 8 | + |
| 9 | + <h3>String</h3> |
| 10 | + We can access a string using indexing. In a string each character is assigned with a unique index value which starts from 0. A string can be written in both single quotes and double quotes. |
| 11 | +Example : 'Hello World'<br> |
| 12 | +"Hello World"<br> |
| 13 | +s = "Hello World"<br> |
| 14 | + |
| 15 | +Python allows negative indexing as well. |
| 16 | +Example : -1, -3, -5. |
| 17 | +Where -1 refers to the last index, -2 refers to second last index and so on. |
| 18 | +Printing here can be done by placing the string in single or double quotes after print. |
| 19 | +<br> |
| 20 | +print("Hello World")<br><br> |
| 21 | + |
| 22 | +<h4>Concatenation</h4> |
| 23 | +A string in python is immutable i.e. it can not be changed once defined. But concatenation is still possible<br> |
| 24 | +s = 'Hello World'<br> |
| 25 | +s = s +"Example<br> |
| 26 | +Now the string will be as "Hello WorldExample".<br> |
| 27 | + <h4>Repetition</h4>This is a unique property of strings in Python programming language. When a string is multiplied with an integer, the characters of string are multiplied the same number of times. |
| 28 | + |
| 29 | +Syntax : string*integer<br> |
| 30 | +s="k"<br> |
| 31 | +s=s*5<br> |
| 32 | +Now the string will be "kkkkk". It has repeated itself 5 times.<br> |
| 33 | +<h4>Slicing</h4>Slicing is done in Python to select or display the desired number of characters in a string. It is done with the help of symbol ':'. |
| 34 | + |
| 35 | +Syntax : String[ index: ] |
| 36 | +<br> |
| 37 | +s="Hello World"<br> 1) s = s[2:]<br> |
| 38 | + |
| 39 | +2. s = s[1:8]<br> |
| 40 | + Here the 1st example will give output "llo World". All the characters from and after second index is selected. |
| 41 | + In the 2nd example output will be "ello Wo". Characters between index number 1 and 8 are selected. |
| 42 | + |
| 43 | +Length of a string can be calculated using the len function. |
| 44 | +Syntax : len("string")<br> |
| 45 | +len("Hello World"<br> |
| 46 | +It will return 11.<br> |
| 47 | + |
| 48 | +### Procedure |
| 49 | + |
| 50 | + <h4>Steps of simulator </h4><br> |
| 51 | + 1. Select the problem.<br> |
| 52 | + 2. Read the simulator details.<br> |
| 53 | + 3. Enter the desired input you want to operate.<br> |
| 54 | + 4. Press CALCULATE to proceed. <br> |
| 55 | + 5. The code will be displayed <br> |
| 56 | + 6. Press NEXT to see the execution of code. <br> |
| 57 | + 7. Relevant line in the code will be highlighted.<br> |
| 58 | + 8. The local variables will be shown in the Output Panel with their values.<br> |
| 59 | + |
| 60 | +### Pre Test |
| 61 | + |
| 62 | +1. Q.1 What is the output of the following code? |
| 63 | + str1="Virtual PythonLab" |
| 64 | + str1[3]='s' |
| 65 | + print(str1) |
| 66 | + <br> |
| 67 | + A.<input type="radio" name="but" id="rb11" onclick="click1();"> Virtual Python Lab |
| 68 | + <br> |
| 69 | + B.<input type="radio" name="but" id="rb12" onclick="click1();"> Virtual |
| 70 | + <br> |
| 71 | + C.<input type="radio" name="but" id="rb13" onclick="click1();"> Error |
| 72 | + <br> |
| 73 | + D.<input type="radio" name="but" id="rb14" onclick="click1();"> Virsual Python Lab |
| 74 | + <br> |
| 75 | + <p id = "p1"></p> |
| 76 | + <br> |
| 77 | +2. What is the output of given string?? |
| 78 | + str1="Virtual" |
| 79 | + str2="Python" |
| 80 | + str3=str1+str2 |
| 81 | + print(len(str3)) |
| 82 | + <br> |
| 83 | + A. <input type="radio" name="but2" id="rb21" onclick="click2();"> 10 |
| 84 | + <br> |
| 85 | + B. <input type="radio" name="but2" id="rb22" onclick="click2();"> 11 |
| 86 | + <br> |
| 87 | + C. <input type="radio" name="but2" id="rb23" onclick="click2();"> 12 |
| 88 | + <br> |
| 89 | + D. <input type="radio" name="but2" id="rb24" onclick="click2();"> 13 |
| 90 | + <br><br> |
| 91 | + <p id = "p2"></p> |
| 92 | + <br> |
| 93 | + |
| 94 | + 3. What arithmetic operators cannot be used with strings? |
| 95 | + <br> |
| 96 | + A. <input type="radio" name="but4" id="rb41" onclick="click4();"> + |
| 97 | + <br> |
| 98 | + B. <input type="radio" name="but4" id="rb42" onclick="click4();"> * |
| 99 | + <br> |
| 100 | + C. <input type="radio" name="but4" id="rb43" onclick="click4();"> - |
| 101 | + <br> |
| 102 | + D. <input type="radio" name="but4" id="rb44" onclick="click4();"> All of the mentioned |
| 103 | + <br><br> |
| 104 | + <p id = "p4"></p> |
| 105 | + <br> |
| 106 | +4. What is th output of "virtual"+1+2+"python" |
| 107 | + <br> |
| 108 | + A. <input type="radio" name="but3" id="rb31" onclick="click3();"> vitual3python |
| 109 | + <br> |
| 110 | + B. <input type="radio" name="but3" id="rb32" onclick="click3();"> Error |
| 111 | + <br> |
| 112 | + C. <input type="radio" name="but3" id="rb33" onclick="click3();"> virtual12python |
| 113 | + <br> |
| 114 | + D. <input type="radio" name="but3" id="rb34" onclick="click3();"> virtualpython |
| 115 | + <br><br> |
| 116 | + <p id = "p3"></p> |
| 117 | + <br> |
| 118 | +5. 5 If y="123"what will be the the return type of type(y)? |
| 119 | + <br> |
| 120 | + A.<input type="radio" name="but" id="rb11" onclick="click1();"> str |
| 121 | + <br> |
| 122 | + B.<input type="radio" name="but" id="rb12" onclick="click1();"> int |
| 123 | + <br> |
| 124 | + C.<input type="radio" name="but" id="rb13" onclick="click1();"> bool |
| 125 | + <br> |
| 126 | + D.<input type="radio" name="but" id="rb14" onclick="click1();"> String |
| 127 | + <br> |
| 128 | + <p id = "p1"></p> |
| 129 | + <br> |
| 130 | + |
| 131 | +### Post Test |
| 132 | + |
| 133 | +1. What is the output of the following code?[A] |
| 134 | + class customer: |
| 135 | + def**init**(self,id): |
| 136 | + self.id=str(id) |
| 137 | + id="2010" |
| 138 | + obj=customer(2567) |
| 139 | + print(obj.id) |
| 140 | + <br> |
| 141 | + A.<input type="radio" name="but" id="rb11" onclick="click1();"> . 2567 |
| 142 | + <br> |
| 143 | + B.<input type="radio" name="but" id="rb12" onclick="click1();"> Error |
| 144 | + <br> |
| 145 | + C.<input type="radio" name="but" id="rb13" onclick="click1();"> 2010 |
| 146 | + <br> |
| 147 | + D.<input type="radio" name="but" id="rb14" onclick="click1();"> None |
| 148 | + <br> |
| 149 | + <p id = "p1"></p> |
| 150 | + <br> |
| 151 | +2. What is the output of following Python program?[B] |
| 152 | + text='Python is high-level programming language' |
| 153 | + words=text.split() |
| 154 | + l=len(words) |
| 155 | + print('Number of words of the text:%d'%l) |
| 156 | + <br> |
| 157 | + A. <input type="radio" name="but2" id="rb21" onclick="click2();"> Error |
| 158 | + <br> |
| 159 | + B. <input type="radio" name="but2" id="rb22" onclick="click2();"> 5 |
| 160 | + <br> |
| 161 | + C. <input type="radio" name="but2" id="rb23" onclick="click2();"> 6 |
| 162 | + <br> |
| 163 | + D. <input type="radio" name="but2" id="rb24" onclick="click2();"> None of these |
| 164 | + <br><br> |
| 165 | + <p id = "p2"></p> |
| 166 | + <br> |
| 167 | + |
| 168 | +3. What is the output when following code is executed?[C] |
| 169 | + str1="virtualpythonlab" |
| 170 | + print(str1[6::-2]) |
| 171 | + <br> |
| 172 | + A. <input type="radio" name="but4" id="rb41" onclick="click4();"> pylab |
| 173 | + <br> |
| 174 | + B. <input type="radio" name="but4" id="rb42" onclick="click4();"> lautriv |
| 175 | + <br> |
| 176 | + C. <input type="radio" name="but4" id="rb43" onclick="click4();"> lurv |
| 177 | + <br> |
| 178 | + D. <input type="radio" name="but4" id="rb44" onclick="click4();"> pati |
| 179 | + <br><br> |
| 180 | + <p id = "p4"></p> |
| 181 | + <br> |
| 182 | + |
| 183 | +4. What is the output of the following code?[D] |
| 184 | + defchangement(str1): |
| 185 | + char=str1[0] |
| 186 | + str1=str1.replace(char,'&') |
| 187 | + str1=char+str1[1:] |
| 188 | + returnstr1 |
| 189 | + print(changement('this is the string')) |
| 190 | + <br> |
| 191 | + A. <input type="radio" name="but3" id="rb31" onclick="click3();"> Syntax Error |
| 192 | + <br> |
| 193 | + B. <input type="radio" name="but3" id="rb32" onclick="click3();"> this is the string |
| 194 | + <br> |
| 195 | + C. <input type="radio" name="but3" id="rb33" onclick="click3();"> &his is &hes &tring |
| 196 | + <br> |
| 197 | + D. <input type="radio" name="but3" id="rb34" onclick="click3();"> this is &he s&tring |
| 198 | + <br><br> |
| 199 | + <p id = "p3"></p> |
| 200 | + <br> |
| 201 | + |
| 202 | +### References |
| 203 | + |
| 204 | +<p style="font-size:100%; margin-top:2%"> |
| 205 | + <br> |
| 206 | + www.learnpython.org/en/Basic_String_Operations |
0 commit comments