Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Saturday, December 8, 2018

String functions in Python

x=6
y=12

print('x==y =',x==y)

This prints false

print('x!=y =',x!=y)

True

print('x>=y =',x>=y)

False

print('x>y =',x>y)

False

print('x<=y =',x<=y)

True
print('x
True


s1='Python\nis\namazing'

Print(s1)

This one prints


Python
is
Amazing

rs1=s1.encode('unicode_escape').decode()
print(rs1)

This one prints Python\nis\namazing


s2='Python\tis\tamazing'

This one prints

 Python    is    amazing

rs2=s2.encode('unicode_escape').decode()
print(rs2)

This one prints

Python\tis\tamazing

s='INfinity'
print(len(s))
print((s).isalpha())
print((s).isdigit())
print((s).upper())
print((s).lower())
print((s).count('i'))
print((s).index('t'))

print('Infinity'.startswith("I"))  -- return true
print('Infinity'.startswith("i")) --false
print('Infinity'.endswith('y'))
print('Infinity'.endswith('Y'))

print(min(30,20,10)) --min
print(max(30,20,10)) --max

print(('a,b,c').split(","))
print(','.join(['a','b','c']))


x=5
y=2
print(x+y)
print(x-y)
print(x*y)
print(x/y)
print(x**y)
print(x%y)

Print Count of vowels using While and For loop in Python

Using while loop:
--------------------

s='Python is a programming language'
count = 0
inc = len(s)
i = 0
while i < inc:
        if s[i] in ('aeiou'):
                count+=1
        else:
                count+=0
        i+=1
print(count)

Using for loop
-----------------

s='Python is a programming language'
count=0
for char in s:
        if char in ('aeiou'):
                count+=1
        else:
                count+=0
print(count)

Range function in Python

Usage of Range function Python
--------------------------------------

This will print sequence numbers from 0-4
0,1,2,3,4

for k1 in range(0,5):
        print(k1)

0 -- Start number
5 -- End number .But 5 will not be included. range print (n-1) as index start from zero.

Below one print number from 10 - 14

for k2 in range(10,15):
        print(k2)

Below one print number 10,12,14,16,18,20

for k3 in range(10,21,2):
        print(k3)
for k4 in range(100,0,-25):
        print(k4)

10 -- starting number
21 -- ending number
2 - step sequence


Below one print the values in list

k1 = list(range(0, 5))
k2 = list(range(10,15))
k3 = list(range(10,21,2))
k4 = list(range(100,0,-25))
print(k1)
print(k2)
print(k3)
print(k4)

list is function which will store the value in a list in sequence.