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

python class topic video