0% found this document useful (0 votes)
2 views3 pages

Using String Methods

The document provides several examples of Python programs that utilize string functions to manipulate and extract information. Examples include removing non-alphabetical or non-numeric characters from input, and extracting gender and birth date from a National Identity Card (NIC) number. The programs demonstrate the use of loops, conditionals, and string methods to achieve their objectives.

Uploaded by

Arshad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Using String Methods

The document provides several examples of Python programs that utilize string functions to manipulate and extract information. Examples include removing non-alphabetical or non-numeric characters from input, and extracting gender and birth date from a National Identity Card (NIC) number. The programs demonstrate the use of loops, conditionals, and string methods to achieve their objectives.

Uploaded by

Arshad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Using String functions

Example 1
'''This program removes all the other characters other
than alphabetical characters and space characters'''
text1=input('Enter a text with some other characters: ')
text2=''
for char in text1:
if [Link]()or [Link]():text2+=char
print(text2)

Example 2
'''This program removes all the other characters other
than numeric characters and decimal point'''
text1=input('Enter a number with some other characters: ')
text2=''
for char in text1:
if [Link]()or char=='.':text2+=char
print(float(text2))

Example3-1
#This program display gender and date of birth when NIC number is given
nic=input('Enter your NIC : ')
year='19'+nic[0:2]
days=int(nic[2:5])
if days<500:
print('Gender is Male')
else:
print('Gender is Female')
days-=500
if days<=31:
m=1
d=days
elif days<=60:
m=2
d=days-31
elif days<=91:
m=3
d=days-60
elif days<=121:
m=4
d=days-91
elif days<=152:
m=5
d=days-121
elif days<=182:
m=6
d=days-152
elif days<=213:
m=7
d=days-182
elif days<=244:
m=8
d=days-213
elif days<=274:
m=9
d=days-244
elif days<=305:
m=10
d=days-274
elif days<=335:
m=11
d=days-305
else:
m=12
d=days-335
print('Year = ',year)
print('Month = ',m)
print('Date = ',d)

Example3-2
#This program display gender and date of birth when NIC number is given
nic=input('Enter your NIC : ')
year='19'+nic[0:2]
days=int(nic[2:5])
if days<500:
print('Gender is Male')
else:
print('Gender is Female')
days-=500
ends=[0,31,60,91,121,152,182,213,244,274,305,335,366]
for end in ends:
if days<end:break
m=[Link](end)
d=days-ends[m-1]
print('Year = ',year)
print('Month = ',m)

Example 3-3
#This program display gender and date of birth when NIC number is given
nic=input('Enter your NIC : ')
if not nic[-1].isalpha():
year=nic[0:4]
days=int(nic[4:7])
else:
year='19'+nic[0:2]
days=int(nic[2:5])
if days<500:
print('Gender is Male')
else:
print('Gender is Female')
days-=500
ends=[0,31,60,91,121,152,182,213,244,274,305,335,366]
for end in ends:
if days<end:break
m=[Link](end)
d=days-ends[m-1]
print('Year = ',year)
print('Month = ',m)
print('Date = ',d)

print('Date = ',d)

You might also like