String
methods in python
Strhand1.py
str = "Python world"
print ("captilize() ",str.capitalize())
#print ("casefold() ",str.casefold()) # python 3.3
print ("swapcase() ",str.swapcase())
print ("title() ",str.title())
print ("lower() ",str.lower())
print ("upper() ",str.upper())
=====================================================================
Strhanh2.py
str2 = "Hello world"
str3 = "Hello2world"
str4 = "World"
str5 = "hello"
str6 = "PYTHON"
dec1 = "10"
dec2 = "10 USD"
print ("Checking if a string contains alpha
numeric string or not:")
condition1 = str2.isalnum();
condition2 = str3.isalnum();
if condition1:
print
(str2," is an alphanumeric string")
else:
print
(str2," is not an alphanumeric string or contains space")
if condition2:
print
(str3," is a alphanumeric string")
else:
print
(str3," is not an alphanumeric string or contains space")
print ("Checking if a string contains alphabets
only:")
condition3 = str2.isalpha();
condition4 = str3.isalpha();
condition5 = str4.isalpha();
if condition3:
print
(str2," is an alphabetic characters")
else:
print
(str2," is not a valid alphabetic characters")
if condition4:
print
(str3," is an alphabetic characters")
else:
print
(str3," is not a valid alphabetic characters")
if condition5:
print
(str4," is an alphabetic characters")
else:
print
(str4," is not a valid alphabetic characters")
print ("Checking if a string contains digits
only:")
condition8 = dec1.isdigit();
condition9 = dec2.isdigit();
if condition8:
print
(dec1," is valid digit(s)")
else:
print
(dec1," is invalid")
if condition9:
print
(dec2," is valid digit(s)")
else:
print
(dec2," is invalid")
print ("Checking if a string contains lower
case characters:")
condition12 = str4.islower();
condition13 = str5.islower();
if condition12:
print
(str4," is valid lower case string")
else:
print
(str4," is not a valid lower case string")
if condition13:
print
(str5," is valid lower case string")
else:
print
(str5," is not a valid lower case string")
print ("Checking if a string contains upper
case characters:")
condition14 = str4.isupper();
condition15 = str6.isupper();
if condition14:
print
(str4," is valid upper case string")
else:
print
(str4," is not a valid upper case string")
if condition15:
print
(str6," is valid upper case string")
else:
print
(str6," is not a valid upper case string")
Strhand3.py
str="Test"
print ("zfill():")
print ("150: ","150".zfill(5))
print ("-150: ","-150".zfill(5))
print ("ljust(s)
:",str.ljust(10,"x"))
print ("rjust(s)
:",str.rjust(10,"x"))
Strhand4.py
str1 = "python world"
str2 = "python is a language"
str3 = "ab c\n\nde fg\rkl\r\n"
str4 = " google google "
str5 = "python python java java"
print ("String: ",str1)
print ("count('t') ",str1.count('t') )
print ("find(t)
",str1.find("t"))
print ("find(w)
",str1.find("w"))
print ("rfind(t)
",str1.rfind("t"))
print ("replace()
",str1.replace("world","python"))
print ("split() ",str2.split(' '))
print ("split('1') ",str2.split(' ', 1 ))
print ("splitlines()",str3.splitlines())
print ("splitlines(True)
",str3.splitlines(True))
print ("lstrip()",str4.lstrip())
print
("lstrip('idiot')",str5.lstrip("idiot"))
print ("rstrip()",str4.rstrip())
print
("rstrip('java')",str5.rstrip("java"))
print
("index('python')",str5.index("python"))
print
("index('python',5)",str5.index("python",5))
print
("rindex('t')",str1.rindex("t"))
print
("rindex('t',5,8)",str1.rindex("t",5,8))
No comments:
Post a Comment