Skip to content

Commit 6b75c12

Browse files
authored
added 2 string programs
1 parent 9629aef commit 6b75c12

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

Extracting_Numbers_from_textString.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Given Sentence
2+
sentence="Ramu have 150 Apples and 10 bananas"
3+
sentence2="Ramu Sell 1 Apple at 15.5 Rupees"
4+
5+
list1=[]
6+
list2=[]
7+
8+
sum=0
9+
sum2=0
10+
# split is used to split sentence
11+
# This code is not fit for Decimal Nos.
12+
13+
print("---------------EXTRACTING NUMBER FROM STRING---------------\n")
14+
for word in sentence.split():
15+
if word.isdigit():
16+
list1.append(word)
17+
18+
print("New List",list1)
19+
20+
21+
22+
# Calculating the SUM
23+
for i in list1:
24+
sum=sum+int(i)
25+
26+
print("No. without Fraction \nsum=",sum,"\n")
27+
28+
# If Decimal Numbers occured in the Sentence
29+
for word2 in sentence2.split():
30+
if not word2.isalpha():
31+
list2.append(word2)
32+
33+
print("New List",list2)
34+
35+
# Calculating the SUM
36+
for j in list2:
37+
sum2=sum2+float(j)
38+
39+
print("No. with Fraction \nsum=",sum2)
40+
print("-----------------------------------------------------------")

substring.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def substring(str1,n):
2+
for i in range(0,n):
3+
for j in range(i+1,n+1):
4+
# Printing the substrings using slicing
5+
# Suppose we have str1="Hello" Str1[0:1] will print "H"
6+
# Str1[1:2] will print "He"
7+
# Str1[1:3] will print "Hel"
8+
# Str1[1:4] will print "Hell"
9+
# Str1[1:5] will print "Hello"
10+
# then i will increment i=2
11+
# Str1[2:3] will print "e"
12+
# Str1[2:4] will print "el"
13+
# And So On
14+
print(str1[i:j])
15+
16+
17+
str1 = input("Enter a String:")
18+
n=len(str1)
19+
substring(str1,n)

0 commit comments

Comments
 (0)