Tuesday, June 30, 2020

String Practice Python

s=input("enter string ")
print("the string in reverse")
l=len(s)
for a in range(-1 ,(-l-1),-1):
    print(s[a],end=' ')

Program Execution:
enter string navodaya
the string in reverse
a y a d o v a n 


s=input("enter string ")
l=len(s)
i=0
for a in range(-1 ,(-l-1),-1):
    print(s[i],"\t",s[a])
    i+=1
output:
enter string school
s 	 l
c 	 o
h 	 o
o 	 h
o 	 c
l 	 s

'a' in 'heya'
True


'jap' in 'japan'
.ipynb
In [1]:
name='superb'
for ch in name: 
   print(ch,'-',end='')
s -u -p -e -r -b -
In [3]:
s=input("enter string ")
print("the string in reverse")
l=len(s)
for a in range(-1 ,(-l-1),-1):
    print(s[a],end=' ')
enter string navodaya
the string in reverse
a y a d o v a n 
In [4]:
s=input("enter string ")
l=len(s)
i=0
for a in range(-1 ,(-l-1),-1):
    print(s[i],"\t",s[a])
    i+=1
enter string school
s 	 l
c 	 o
h 	 o
o 	 h
o 	 c
l 	 s
In [2]:
'a' in 'heya'
Out[2]:
True
In [3]:
'jap' in 'japan'
Out[3]:
True
In [6]:
'paj' not in 'japan'
Out[6]:
True
In [4]:
ord('a')
Out[4]:
97
In [5]:
ord('A')
Out[5]:
65
In [6]:
ord('1')
Out[6]:
49
In [12]:
word='amazing'
In [7]:
chr(65)
Out[7]:
'A'
In [13]:
word
Out[13]:
'amazing'
In [15]:
word[-5:-1]
Out[15]:
'azin'
In [16]:
word[0:-1]
Out[16]:
'amazin'
In [15]:
w="india"
w[0:]
Out[15]:
'india'
In [17]:
word[0:]
Out[17]:
'amazing'
In [20]:
word[-len(word):5]
Out[20]:
'amazi'
In [21]:
len(word)
Out[21]:
7
In [17]:
 
Out[17]:
'aidn'
In [24]:
word[0]
Out[24]:
'a'
In [25]:
word[-1]
Out[25]:
'g'
In [26]:
s1='spam'
s2='ni'
print("hello,"+s2)
print(3*s1+2*s2)
print(s1[1])
hello,ni
spamspamspamnini
p
In [18]:
s=input("enter string")
l=len(s)
c=0
for i in range(l):
    if s[i]in'aeiou':
        c+=1
        
        
print("number of vowelis  ",c)
enter stringnavodaya
number of vowelis   4
In [25]:
s="india is my country"
t=s.split()
print(s)
print(t)
print(len(s),len(t))
india is my country
['india', 'is', 'my', 'country']
19 4
In [29]:
'apple'>'pineapple'
Out[29]:
False
In [30]:
'ab'>'XY'
Out[30]:
True
In [31]:
'Ax'>'aX'
Out[31]:
False
In [33]:
'apple'in 'pineapple'
Out[33]:
False
In [34]:
'pine' in 'Pineapple'
Out[34]:
False
In [35]:
s='apple'
In [37]:
s[:-1]+s[-1:]
Out[37]:
'apple'
In [38]:
s='#'
p=''
for a in range(5):
    p+=s
    print(p)
#
##
###
####
#####
In [40]:
n=int(input('enter number'))
s='*'*n
for i in range(n):
    print(s)
    s=s[:-1]
    
enter number5
*****
****
***
**
*
In [41]:
sr='kundeshwar'
sr.capitalize()
Out[41]:
'Kundeshwar'
In [43]:
sr.find('d')
Out[43]:
3
In [45]:
sr='--===='
sr.isalnum()
Out[45]:
False
In [46]:
sr.isalpha()
Out[46]:
False
In [48]:
sr.isdigit()
Out[48]:
False
In [49]:
sr.isspace()
Out[49]:
False
In [50]:
sr.isupper()
Out[50]:
False
In [51]:
sr.islower()
Out[51]:
False
In [52]:
sr='hello'
print(sr.lower())
print(sr.upper())
hello
HELLO
In [ ]:
line=input("enter line")
x=line.split()
y=''
for i in range(len(x)):
    x[i]=x[i].upper()
    y+=x[i]
print(y)    
In [ ]:
 
In [3]:
line=input("enter line")
x=line.split()
y=''
for i in range(len(x)):
    x[i]=x[i].upper()
    y+=' '+x[i]
print(y)
enter linei am your
 I AM YOUR
In [ ]:
s=input("enter phone number with in format stdcode-areacode-number stdcode of 3 digit area code of three digit ad number of 4 digit")
if s[0:4].isdigit():
  print("areacode is right")
In [ ]:
#programt to enter a phone number with 10 digit and two dashes with dashes after area code and next three numbers 
#for example 017-555-1212 is valid 
s=input("enter phone number with in format stdcode-areacode-number stdcode of 3 digit area code of three digit ad number of 4 digit")
if s[0:4].isdigit():
  print("areacode is right")s=input("enter phone number with in format stdcode-areacode-number stdcode of 3 digit area code of three digit ad number of 4 digit")
if len(s)==12 and s[0:3].isdigit() and s[4:7].isdigit() and s[8:].isdigit()and s[3]=='-'and s[7]=='-' :
    print('string is ok')
else:
    print('incorrect string')
    
In [ ]:
#program to enter a string extract all digit from string ,sum all digit in string
#print origianl string ,the digit ,the sum of digit
#if no digit print 'has no digit'
#if input is 'abc123' than output willbe
#print abc123 has the digits 123 which sum to 6
str=input('enter string')
sum=0
dstr=''
for i in range(len(str)):
    if str[i].isdigit():
        sum+=int(str[i])
        dstr+=str[i]
print(str,'has the digits',dstr,'which sum is',sum)
In [ ]:
#program to enter a string extract all digit from string ,sum all digit in string
#print origianl string ,the digit ,the sum of digit
#if no digit print 'has no digit'
#if input is 'abc123' than output willbe
#print abc123 has the digits 123 which sum to 6
str=input('enter string')
sum=0
dstr=''
for i in range(len(str)):
    if str[i].isdigit():
        sum+=int(str[i])
        dstr+=str[i]
print(str,'has the digits',dstr,'which sum is',sum)
In [2]:
#9c3 program to enter a string and print number of word ,character in string
s=input('enter string')
sp=s.split()
cw=len(sp)
cch=len(s)
an=0
print(s,'has number of word',cw,'and number of character',cch)
for i in range(len(s)):
      if s[i].isalnum():
          an+=1
print('number of alphanumeric is ',an,'and its percentage is',(an/cch)*100)
enter stringererew5656--- 23443  234234 fghg
ererew5656--- 23443  234234 fghg has number of word 4 and number of character 32
number of alphanumeric is  25 and its percentage is 78.125
In [ ]:
#9c5 input one integer and one string ,collect digit sequence from string
#then sum digit sequence and integer
n=int(input('enter number'))
s=input('enter a string')
sd=''
for i in range(len(s)):
    if s[i].isnumeric():
        sd+=s[i]
        
print('the sum is ',n+int(sd))
In [ ]:
#9c6 write program to input two string and print small string first then
#print large string in format ex input string is panda python then output will be
#panda
#p        n
# y     o
#   th

s=input('enter first string')
s1=input('enter second string')
ls=len(s)
ls1=len(s1)
if(ls>ls1):
    s,s1=s1,s
    ls,ls1=ls1,ls
print(s)
for i in range(int(ls1/2)+1):
      print(' '*i,s1[i],' '*(ls1-1-2*i),s1[ls1-1-i])

No comments:

Post a Comment