string concepts in python part 3

By
String operations in python:
There are many Built-in string function available in python language.Here We will learn content test functions with examples as given below:-
1.) isalpha() :-It returns True ,if all characters in the string are alphabets otherwise False. The alphabets can be lower case and upper case both.This function doesn't take any parameters.If string contains a white space or any numeric value then this function returns False.
Syntax:-
String.isalpha()

e.g.
#type1:
student='ramashanker'
name=student.isalpha()
print("type1 returns =",name)
#type2:
student='MOHAN'
name=student.isalpha()
print("type2 returns =",name)
#type3:
student="Ramashanker Verma"
print("type3 returns =",student.isalpha())
#type4:
student='RAM123'
name=student.isalpha()
print("type4 returns =",name)
#type5:
student='12345'
name=student.isalpha()
print("type5 returns =",name)
#type6:
student='Ramesh'
if(student.isalpha()== True):
    print("All characters in string are alphabets")
else:
     print("All characters in string are not alphabets")        
Output:-
type1 returns = True
type2 returns = True
type3 returns = False
type4 returns = False
type5 returns = False
All characters in string are alphabets
2.) isalnum():- It returns True,if the characters in the string are alphanumeric(either alphabets or numbers) otherwise False.This function doesn't take any parameters.
Syntax:-
String.isalnum()
e.g.
#type1:
student='ramashanker'
name=student.isalnum()
print("type1 returns =",name)
#type2:
student='MOHAN123'
name=student.isalnum()
print("type2 returns =",name)
#type3:
student="Ramashanker 123"
print("type3 returns =",student.isalnum())
#type4:
student='RAM123@'
name=student.isalnum()
print("type4 returns =",name)
#type5:
student='12345'
name=student.isalnum()
print("type5 returns =",name)
#type6:
student='Ramesh'
if(student.isalnum()== True):
    print("All characters in the string are alphanumeric")
else:
     print("All characters in string are not alphanumeric")
Output:-
type1 returns = True
type2 returns = True
type3 returns = False
type4 returns = False
type5 returns = True
All characters in the string are alphanumeric
3.) isdigit() :- It returns True,if all characters in the string are digits otherwise False.This function doesn't take any parameters.
Syntax:-
String.isdigit()
e.g.
#type1
a='12345'
number=a.isdigit()
print("The values in the string(a)are digits(type1): ",number)
#type2
b='123 256 234'
number=b.isdigit()
print("The values in the string(b)are digits(type2): ",number)
#type3
c='ram12345'
number=b.isdigit()
print("The values in the string(c)are digits(type3): ",number)
#type4
#d=22456
d='\u00B22456'
number=d.isdigit()
print("The values in the string(d)are digits(type4): ",number)
#type5
#e='1/2'
e='\u00BD'
number=e.isdigit()
print("The values in the string(e)are digits(type5): ",number)
Output:-
The values in the string(a)are digits(type1):  True
The values in the string(b)are digits(type2):  False
The values in the string(c)are digits(type3):  False
The values in the string(d)are digits(type4):  True
The values in the string(e)are digits(type5):  False
Note:-
  • The suberscript and subscripts are also considered as digit characters if it is written in Unicode .The isdigit() function returns True
  • The roman numerals,currency numerators and fractional are considered numeric characters not digits(written in Unicode).The isdigit() function returns False.  
4.) isdecimal() :- It returns true,if all characters in string are decimals otherwise false.if at least one character in the string is not decimal then it returns False value.This function doesn't take any parameters.
Syntax:-
String.isdecimal()
e.g.
#type1
a='12345'
number=a.isdecimal()
print("The values in the string(a)are decimal(type1): ",number)
#type2
b='123 256 234'
number=b.isdecimal()
print("The values in the string(b)are decimal(type2): ",number)
#type3
c='ram12345'
number=b.isdecimal()
print("The values in the string(c)are decimal(type3): ",number)
#type4
#d=22456
d='\u00B22456'
number=d.isdecimal()
print("The values in the string(d)are decimal(type4): ",number)
#type5
#e='1/2'
e='\u00BD'
number=e.isdecimal()
print("The values in the string(e)are decimal(type5): ",number)
Output:-
The values in the string(a)are decimal(type1):  True
The values in the string(b)are decimal(type2):  False
The values in the string(c)are decimal(type3):  False
The values in the string(d)are decimal(type4):  False
The values in the string(e)are decimal(type5):  False
5.) islower():- It returns true ,if all character in string are lower lower case alphabets otherwise false.This function doesn't take any parameters.
Syntax:-
String.islower()
e.g.
str1='hello friends,how are you?'
str2='WELCOME TO MNT LAB'
str3='WELCOME to MNT LAB'
print("After check str1 value is= ",str1.islower())
print("After check str2 value is= ",str2.islower())
print("After check str3 value is= ",str3.islower())
Output:-
After check str1 value is=  True
After check str2 value is=  False
After check str3 value is=  False
6.) isupper() :- It returns true, if all characters in string are upper case alphabets otherwise false.This function doesn't take any parameters.
Syntax:-
String.isupper()
e.g.
str1='hello friends,how are you?'
str2='WELCOME TO MNT LAB'
str3='WELCOME to MNT LAB'
print("After check str1 value is= ",str1.isupper())
print("After check str2 value is= ",str2.isupper())
print("After check str3 value is= ",str3.isupper())
Output:-
After check str1 value is=  False
After check str2 value is=  True
After check str3 value is=  False
7.) isnumeric():- It returns true,if all characters in string are numeric characters(like :decimal,digits(superscript,subscript),numeric) and unicode characters(like:roman numerals,currency numerals and fraction) otherwise false.This function doesn't take any parameters.
Syntax:-
String.isnumeric()
e.g.
str1='123456'
print("Str1 value is= ",str1.isnumeric())
str2='12.45'
print("Str2 value is= ",str2.isnumeric())
str3='\u00BD4562' #uncode superscript of 4562
print("Str3 value is= ",str3.isnumeric())
str4='\u00BC'    #unicode fraction of 1/4
print("Str4 value is= ",str4.isnumeric())
str5='ram1234'
print("Str5 value is= ",str5.isnumeric())

if str3.isnumeric()==True:
    print("The Value of str3 variable is a numeric value")
else:
     print("The Value of str3 variable is not a numeric value")
Output:-
Str1 value is=  True
Str2 value is=  False
Str3 value is=  True
Str4 value is=  True
Str5 value is=  False
The Value of str3 variable is a numeric value
8.) isidentifier():- it returns true,if the string is a valid identifier  otherwise false.This function doesn't take any parameters.
Syntax:-
String.isidentifier()
e.g.
#type1
str1='name1'
a=str1.isidentifier()
print("str1 is valid identifier",a)
#type2
str2='name_1'
b=str2.isidentifier()
print("str2 is valid identifier",b)
#type3
str3='name 1'
c=str3.isidentifier()
print("str3 is valid identifier",c)
#type4
str4='1234'
d=str4.isidentifier()
print("str4 is valid identifier",d)
#type5
str5='@name'
print("str5 is valid identifier",str5.isidentifier())
#type6
str6='name12'
if(str6.isidentifier()==True):
    print("str6 value is a valid identifier")
else:
     print("str6 value is not a valid identifier")

Output:-
str1 is valid identifier True
str2 is valid identifier True
str3 is valid identifier False
str4 is valid identifier False
str5 is valid identifier False
str6 value is a valid identifier
9.) isspace():-It returns true,if the characters in string are some white space otherwise false.for examples tabs,spaces,newline etc.This function doesn't take any parameters.
There are some white space characters as given below:-
' ' --> space
\n-->newline
\t-->horizontal tab
\v-->vertical tab
\r-->carriage return
Syntax:-
String.isspace()
e.g.
#type1
str1="Welcome to mnt \n"
a=str1.isspace()
print("All characters in string(str1) contains white spaces= ",a)
#type2
str2="\t\n\r\v"
b=str2.isspace()
print("All characters in string(str2)contains white spaces= ",b)
#type3
str3="Crona virus is more Dangerous ''for people"
c=str3.isspace()
if c==True:
    print("All characters in string(str3) contains white spaces")
else:
  print("All characters in string(str3)doesn't contain white spaces")
#type4
str4=' '
d=str4.isspace()
print("All characters in string(str4)contains white spaces= ",d)
Output:-
All characters in string(str1) contains white spaces=  False
All characters in string(str2)contains white spaces=  True
All characters in string(str3)doesn't contain white spaces
All characters in string(str4)contains white spaces=  True
10.) istitle():- It returns true,if the string has title property otherwise false.means if any string first character is capital latter and remaining 
characters are in lowercase,then it is called title string.This function doesn't take any parameters.
Syntax:-
String.istitle()
 e.g.
#type1
str1="Welcome To Mnt Lab"
a=str1.istitle()
print("String(str1)has title properties= ",a)
#type2
str2="Welcome to mnt lab"
b=str2.istitle()
print("String(str2)has title properties= ",b)
#type3
str3="Msdotnet"
c=str3.istitle()
print("String(str3)has title properties= ",c)
#type4
str4="MSDOTNET"
d=str4.istitle()
print("String(str4)has title properties= ",d)
#type5
str5="Welcome To Home"
e=str5.istitle()
if(e==True):
    print("String(str5) has titled property")
else:
     print("String(str5) has not titled property")
Output:-
String(str1)has title properties=  True
String(str2)has title properties=  False
String(str3)has title properties=  True
String(str4)has title properties=  False
String(str5) has titled property
Watch Complete Lecture 14 Video:-
String in python Lecture 14

There are some conversions functions available in python strings as given below:-
1.) upper():-It is used to convert the given string to the upper case alphabets latter.This function doesn't take any parameters.
Syntax:-
String.upper()
e.g.
#type1
str1='welcome to msdotnet'
a=str1.upper()
print("string str1 =",a)
#type2
str2='MNT LAB'
b=str2.upper()
print("string str2 =",b)
#type3
str3='MNT LAB'
str4='mnt lab'
if(str3==str4.upper()):
    print("Both strings are equals")
else:
    print("Both strings are not equals")
Output:-
string str1 = WELCOME TO MSDOTNET
string str2 = MNT LAB
Both strings are equals
2.) lower():-This function is used to convert the given strings to the lower case alphabets latter.This function doesn't take any parameters.
Syntax:-
String.lower()
e.g.
#type1
str1='WELCOME TO MNT LAB'
a=str1.lower()
print("string str1 =",a)
#type2
str2='msdotnet.co.in'
b=str2.lower()
print("string str2 =",b)
#type3
str3='mnt lab'
str4='MNT LAB'
if(str3==str4.lower()):
    print("Both strings are equals")
else:
    print("Both strings are not equals")
Output:-
string str1 = welcome to mnt lab
string str2 = msdotnet.co.in
Both strings are equals
3) Capitalize():-This function returns a string with first letter capitalized and all remaining characters lower cased. This function doesn't take any parameters.
Syntax:-
String.capitalize()
e.g.
#type1
str1='WELCOME TO MNT LAB'
a=str1.capitalize()
print("string str1 =",a)
#type2
str2='msdotnet.co.in'
b=str2.capitalize()
print("string str2 =",b)
#type3
str3='Mnt lab'
str4='MNT LAB'
if(str3==str4.capitalize()):
    print("Both strings are equals")
else:
    print("Both strings are not equals")
Output:-
string str1 = Welcome to mnt lab
string str2 = Msdotnet.co.in
Both strings are equals
4.) swapcase():- This swapcase() function converts all uppercase characters to lowercase and all lowercase characters to uppercase of the given string .This function doesn't take any parameters.
Syntax:-
String.swapcase()
e.g.
#type1
str1='WELCOME TO MNT LAB'
a=str1.swapcase()
print("string str1 =",a)
#type2
str2='msdotnet.co.in'
b=str2.swapcase()
print("string str2 =",b)
#type3
str3='mnt lab'
str4='MNT LAB'
if(str3==str4.swapcase()):
    print("Both strings are equals")
else:
    print("Both strings are not equals")
#type4
str5='mNT lAB'
c=str5.swapcase()
print("string str5 =",c)
Output:-
string str1 = welcome to mnt lab
string str2 = MSDOTNET.CO.IN
Both strings are equals
string str5 = Mnt Lab
5.) format(value) :-It returns a formatted version of string of pass value. format() function takes any number of parameters but it is divided in two type of parameter.
  • Keyword parameters:-This parameter is key=value type.it can be accessed with key parameter inside curly braces {key}.
  • Positional parameters:- This parameter can be accessed by index of parameter inside curly braces {index}
Syntax:-
String.format(P0,P1,P2...)
String.format(K1=V1,K2=V2,K3=V3...)
e.g.
name="mohan"
age='32'
income=2000
print("hello,{} You are {} year old andyour income is rs {}".format(name,age,income))
print("My name is {},i am {} year old and my income is rs {}".format(name,age,income))
print("hello,{0} You are {1} year old.your salary is rs {2}".format(name,age,income))
print("hello,{2} You are {0} year old.your salary is rs {1}".format(age,income,name))
Output:-
hello,mohan You are 32 year old andyour income is rs 2000
My name is mohan,i am 32 year old and my income is rs 2000
hello,mohan You are 32 year old.your salary is rs 2000
hello,mohan You are 32 year old.your salary is rs 2000
Special Notes:- Today we will learn Addition of String integer value and integer value with an example:-
  • If we add two string integer value,the second string integer value add just right of the first string integer value.
  • If we add two integer value,addition arithmetic operation is performed on both value and return a new integer value. 
e.g.
#difference between string ineger addtion(concatenation) and integer value addition.
a='1256'
b='4500'
c=a+b
print("Addition of two string integer addition value=",c)
p=1256
q=4500
r=p+q
print("Addition of two integer  addition value=",r)
print("See both value are different")
Output:-
Addition of two string integer addition value= 12564500
Addition of two integer  addition value= 5756
See both value are different
For More...
  1. Python history,features and its applications in real life 
  2. How to install python setup in different os
  3. Python programming modes
  4. Token,comment,indentation and multi-lining comments in python 
  5. python variable declaration and assignments.
  6. oops concepts in c#
  7. Call by value and call by reference in c#
Watch Complete Lecture 15 Video


0 comments:

Post a Comment

Powered by Blogger.