String - All in One
String - All in One
Example1 :
s1 = "PYTHON"
print(s1)
================================ RESTART ================================
>>>
PYTHON
>>>
Example2 :
s1 = "PYTHON"
for ch in s1:
print(ch, end=' ')
================================ RESTART ================================
>>>
PYTHON
>>>
Example 3 : OF Forward indexing:
Another to print A String
s1 = "PYTHON"
n = len(s1)
print(“Length of S1 = ”, n)
for i in range (0,n):
print(s1[i], end=' *')
================================ RESTART ================================
>>>
P*Y*T*H*O*N*
>>>
Explain :
S1[i] = value
S1[0] = P
Page 1 of 6
S1[1] = Y
S1[2] = T
S1[3] = H
S1[4] = O
S1[5] = N
print(s1[-1:0:-1])
Output
'dlroW olle'
String is Class , and functions of class are always access by prefixing of dot operator.
What Is Class ? A class is collection of function.
What is object ? It is reference Variable which is used to access the function of class
What is the use of Dot (.) Operator ? It is used to provide accessibility of members inside the class
s1='12d34'
>>> s1.isalnum()
True
4. isdigit : tell all values are numeric
s1='1234'
>>> s1.isdigit()
True
5. isalpha
s2='aabc'
>>> s2.isalpha()
True
>>> s2='12aabc'
>>> s2.isalpha()
False
>>>
6. isspace : Find the space in string.
s1="My School"
>>> s1.isspace()
False
>>> s1=' '
>>> s1.isspace()
True
Example :
s1 = input("Enter First Name")
flg=True
for ch in s1:
if ch.isspace():
flg=False
if flg==True:
print("String has No Space")
else:
print("String has a Space")
OUTPUT
Enter First NameAnashin Bhardwaj
String has a Space
>>>
OUTPUT 2
Enter First NameAnashin
String has No Space
7. islower :
>>> s1='wisdom'
Page 3 of 6
>>> s2='SCHOOL'
>>> s1.islower()
True
>>> s2.islower();
False
8. isupper
>>> s1='wisdom'
>>> s2='SCHOOL'
>>> s1.isupper()
False
>>> s2.isupper()
True
>>>
9. istitle
>>> s1='python is a completer language'
>>> s1.istitle()
False
>>> s1='Python Is A Completer Language'
>>> s1.istitle()
True
>>>
10. lower
>>> s1='PYTHON'
>>> s1.lower()
'python'
>>>
11. upper
>>> s1='python'
>>> s1.upper()
'PYTHON'
>>>
12. title
s1='python is a completer language'
>>> s1.title()
'Python Is A Completer Language'
Home Work
A
bC
dEf
GhIj
KlMnO
Hint : islower, isupper, lower, upper, if, loop
List : Tomorrow we will discuss about list.
Page 4 of 6
Program :
Write a program in Language Python on the following basis
Given a word, Check whether if is is a valid password or not by using a regular expression. A password is
said to be “Good” if it formed by the following rules
i. Should begin with a letter
ii. Should contain atleast one digit and one special character(these are “@” , “_”)
iii. Atleast one character should be in upper case.
iv. Length of the password should be atleast 8.
Print “Bad” if the given word do not follow the above rules
s1 = input("Enter a String")
length = len(s1)
#print(length)
first = False
digit=False
spchar = False
upchar = False
if s1[0].isalpha()== True:
first = True
for ch in s1:
if ch.isupper()==True:
upchar = True
if ch=='_' or ch=='@' :
spchar=True
if ch.isdigit() == True:
digit=True;
if spchar == True and upchar == True and first == True and digit == True and length>=8 :
print("Good")
else:
print ("Bad")
OUTPUT:
Enter a StringAnasinh@321
Good
OUTPUT 2 :
Enter a Stringanasinh321
Bad
Home Work : Write a program to count and prints its statistics as given below
Number of Upper Letters
Number of Lower Letters
Number of Alphabets
Number of digits
Number of Spaces.
Page 5 of 6
Page 6 of 6