﻿ Python Sample Papers with Solution

## Python String Exercise

``````
Solution
import numpy as np
def getMid3(TestStr):
Mid_Indx = int(len(TestStr) /2)
print("Org :", TestStr)
Mid3 = TestStr[Mid_Indx-1:Mid_Indx+2]
print("Middle 3 chars :", Mid3)
getMid3("DataScience")
getMid3("Machine")
```
```

### Output

>>>
Org : DataScience
Middle 3 chars : Sci
Org : Machine
Middle 3 chars : chi

``````
Mid_Indx = int(len(s1) /2)
print("Org: ", s1, s2)
Mid3 = s1[:Mid_Indx-1:]+ s2 +s1[Mid_Indx-1:]
print("After inserting new str : ", Mid3)

```
```
>>>
Org: MachinesLearning are
After inserting new str : MachinesareLearning

``````

def strFun(s1, s2):
resultString = s1[:1] + s2[:1] + s1[int(len(s1) /2):int(len(s1) /2)+1] +s2[int(len(s2) /2):int(len(s2) /2)+1] +s1[len(s1)-1] + s2[len(s2)-1]
print("Mixed Str is ", resultString)
s1 = "Machine"
s2 = "Science"
strFun(s1, s2)
```
```
>>>
Mixed Str is MSheee

``````
inputStr = " PythonClass "
words = inputStr.split()
lower = []
upper = []
for char in inputStr:
if char.islower():
lower.append(char)
else:
upper.append(char)
sortedStr = ''.join(lower + upper)
print("\n final str with lowercase letters comes first:")
print(sortedStr)
```
```
>>>
final str with lowercase letters comes first
ythonlassPC

``````
def myFun(str):
words = str.split()
charCnt = 0
digitCnt = 0
symbCnt = 0
for char in str:
if char.islower() or char.isupper():
charCnt+=1
elif char.isnumeric():
digitCnt+=1
else:
symbCnt+=1

print("Chars = ", charCnt, "Digits = ", digitCnt, "Symbol = ", symbCnt)

str = "D@#zo28ne^\$j4pr"
print(" total counts of chars, digits,and symbols")
print(myFun(str))
```
```
>>>
total counts of chars, digits,and symbols
Chars = 8 Digits = 3 Symbol = 4

``````
def strFun(s1, s2):
s2 = s2[::-1]
lengthS1 = len(s1)
lengthS2 = len(s2)
length = lengthS1 if lengthS1 > lengthS2 else lengthS2
resultString=""
for i in range(length):
if(i < lengthS1):
resultString = resultString + s1[i]
if(i < lengthS2):
resultString = resultString + s2[i]

print(resultString)

s1 = "DZoneJpr"
s2 = "Website"
strFun(s1, s2)
```
```
>>>
Mix String is DeZtoinsebJepWr

``````
def BalStr(s1, s2):
flag = True
for char in s1:
if char in s2:
continue
else:
flag = False
return flag

s1 = "zr"
s2 = "Dzonejpr"
flag = BalStr(s1, s2)
print("s1 and s2 are balanced", flag)

s1 = "zrt"
s2 = "Dzonejpr"
flag = BalStr(s1, s2)
print("s1 and s2 are balanced", flag)
```
```
>>>
s1 and s2 are balanced True
s1 and s2 are balanced False

``````
import re

inputStr = "English = 88 Science = 80 Math = 90 History = 60"
markList = [int(num) for num in re.findall(r'\b\d+\b', inputStr)]
totalMarks = 0
for mark in markList:
totalMarks+=mark

percentage = totalMarks/len(markList)
print("Total Marks is:", totalMarks, "Percentage is ", percentage)
```
```
>>>
Total Marks is: 318 Percentage is 79.5

``````
inputString = "Welcome to Machine Learning, automated Machine!"
substring = "Machine"
tempString = inputString.lower()
count = tempString.count(substring.lower())
print("count of Machine is:", count)
```
```
>>>
count of Machine is: 2

``````
inputStr = "dzonejprdzonejprdzone"
countDict = dict()
for char in inputStr:
count = inputStr.count(char)
countDict[char]=count
print(countDict)

```
```
>>>
{'d': 3, 'z': 3, 'o': 3, 'n': 3, 'e': 3, 'j': 2, 'p': 2, 'r': 2}