Sunday, November 10, 2019

merging strings in python

s1=input("enter the first string::")
s2=input("enter the second string::")
output=''
i=j=0
while i<len(s1) or j<len(s2):
    output=output+s1[i]+s2[j]
    i=i+1
    j=j+1
print(output)

output:
enter the first string::anil

enter the second string::durg
adnuirlg


example 2:
s1=input("enter the first string::")
s2=input("enter the second string::")
output=''
i=j=0
while i<len(s1) or j<len(s2):
    if i<len(s1):
        output=output+s1[i]
        i=i+1
    if i<len(s2):
        output=output+s2[j]
        j=j+1
print(output)

output:
enter the first string::anil

enter the second string::durgam
adnuirlgam

printing unicode character in place of numbers in python

s= input("Enter some string:")
output=''
for x in s:
    if x.isalpha():
        output=output+x
        previous=x
    else:
        newch=chr(ord(previous)+int(x))
        output=output+newch
print(output)

output:
Enter some string:a4b2d9
aebddm

using sorted keyword and extracting alphabets and numbers using python

s= input("Enter some string:")
s1=s2=output=''
for x in s:
    if x.isalpha():
        s1=s1+x
    else:
        s2=s2+x
print(s1)
print(s2)


output:
Enter some string:a1b2c3
abc
123

example 2:
s= input("Enter some string:")
s1=s2=output=''
for x in s:
    if x.isalpha():
        s1=s1+x
    else:
        s2=s2+x
for x in sorted(s1):
    output=output+x
for x in sorted(s2):
    output=output+x
print(output)

output:
Enter some string:a1b2c3d4
abcd1234


example 3:
s= input("Enter some string:")
output=''
for x in s:
    if x.isalpha():
        output=output+x
        previous=x
    else:
        output=output+previous*(int(x)-1)
print(output)


output:
Enter some string:a4b2c5d2
aaaabbcccccdd

Wednesday, November 6, 2019

printing the character at even and odd positions

# -*- coding: utf-8 -*-
"""
Created on Thu Nov  7 07:46:59 2019

@author: Onyx1
"""

a=input("Enter some string::")
i=0
print("The Characters at even positions:")
while i<len(a):
    print(a[i],end=',')
    i=i+2
i=1
print("The Characters at odd positions:")
while i<len(a):
    print(a[i],end=',')
    i=i+2

output:
Enter some string::anildurgam
The Characters at even positions:
a,i,d,r,a,The Characters at odd positions:
n,l,u,g,m,




example2:
a=input("Enter some string::")
i=0
print("The Characters at even positions:")
while i<len(a):
    print(a[i],end=',')
    i=i+2
i=1
print()
print("The Characters at odd positions:")
while i<len(a):
    print(a[i],end=',')
    i=i+2

output:
Enter some string::anildurgam
The Characters at even positions:
a,i,d,r,a,
The Characters at odd positions:
n,l,u,g,m,

printing the character at even and odd positions in python

a=input("enter some string::")
print("Character of the even positions",a[::2])
print("Character of the even positions",a[1::2])


output:
enter some string::anil
Character of the even positions ai
Character of the even positions nl

reverse the given string using slice operator


a=input("Enter some string::")
print(a[::-1])

output:
Enter some string::anil durgam
magrud lina


method 2:
 using inbuilt functions


a=input("Enter some string::")
for x in reversed(a):
    print(x)

output:
Enter some string::anil durgam
m
a
g
r
u
d

l
i
n
a

join method

a=input("Enter some string::")
print(''.join(reversed(a)))

output:
Enter some string::anil
lina


without new line of the reversed string

a=input("Enter some string::")
for x in reversed(a):
    print(x,end='')

output:
Enter some string::anil
lina

Tuesday, November 5, 2019

format method in python

# -*- coding: utf-8 -*-
"""
Created on Wed Nov  6 07:28:23 2019
@author: Anil Durgam
"""

name="anil"
age=29
salary = 20000
print("{}'s age is {} and his salary is{}".format(name,age,salary))
print("{0}'s age is {1} and his salary is{2}".format(name,age,salary))
print("{x}'s age is {y} and his salary is{z}".format(x=name,y=age,z=salary))
print("{x}'s age is {y} and his salary is{z}".format(z=salary,x=name,y=age))

output:
anil's age is 29 and his salary is20000
anil's age is 29 and his salary is20000
anil's age is 29 and his salary is20000
anil's age is 29 and his salary is20000

isalnum,isalpha,islower,isspace methods in python


"""
Created on Sun Nov  3 09:54:15 2019

@author: anil durgam
"""

s=input("Enter any character:")
if s.isalnum():
    print("Alpha numeric character")
    if s.isalpha():
        print("Alphabet character")
        if s.islower():
            print("Lower case alphabet character")
        else:
            print("Upper case alphabet character")
    else:
        print("It is a digit")
elif s.isspace():
    print("It is space character")
else:
    print("Non space Special character")


output:

Enter any character:5
Alpha numeric character
It is a digit



Enter any character:a
Alpha numeric character
Alphabet character
Lower case alphabet character


Enter any character:
It is space character
    

Sunday, November 3, 2019

startwith,endswith methods in python

Example 1:

s="Learning Python is very easy"
print(s.startswith("Learning"))
print(s.endswith("Easy"))
print(s.endswith("easy"))

output:
True
False
True

upper,lower,title,swapcase,capitalize methods in python


example 1:

s="Anil Durgam Learning Python"
print(s.upper())
print(s.lower())
print(s.swapcase())
print(s.title())
print(s.capitalize())

output:
ANIL DURGAM LEARNING PYTHON
anil durgam learning python
aNIL dURGAM lEARNING pYTHON
Anil Durgam Learning Python
Anil durgam learning python

join method in python

join method is used for to join the strings . especially used for the list and tuples

example 1:
list joining using join method
s=["anil", "durgam", "embedded"]
l="-".join(s)
print(l)

output:
anil-durgam-embedded

example 2:
tuple joining with join method
s=("anil", "durgam", "embedded")
l="-".join(s)
print(l)

output:
anil-durgam-embedded

example 3:
s=("anil", "durgam", "embedded")
l=":".join(s)
print(l)

output:
anil:durgam:embedded

example 4:
s=("anil", "durgam", "embedded")
l=" ".join(s)
print(l)

output:
anil durgam embedded

example 5:
s=("anil", "durgam", "embedded")
l="".join(s)
print(l)

output:
anildurgamembedded

python class topic video