article

Sunday, May 29, 2016

Python String and Variables Example Code

Python String and Variables Example Code
 
#!/usr/bin/python

str = 'Hello World!'
print str          # Prints complete string
print str[0]       # Prints first character of the string
print str[2:5]     # Prints characters starting from 3rd to 5th
print str[2:]      # Prints string starting from 3rd character
print str * 2      # Prints string two times
print str + "TEST" # Prints concatenated string
#------------------------------------------------------------------
str = "this is string example....wow!!!";
print "str.capitalize() : ", str.capitalize()
#------------------------------------------------------------------
# This is a comment.
#Syntax str.center(width[, fillchar]) width -- This is the total width of the string. fillchar -- This is the filler character.
str = "this is string example....wow!!!";
print "str.center(40, 'a') : ", str.center(40, 'a')
#------------------------------------------------------------------
#string count
#Syntax str.count(sub, start= 0,end=len(string))
str = "this is string example....wow!!!";
sub = "i";
print "str.count(sub, 4, 40) : ", str.count(sub, 4, 40)
sub = "wow";
print "str.count(sub) : ", str.count(sub)
#------------------------------------------------------------------
#string len
str = "this is string example....wow!!!";
print "Length of the string: ", len(str);
#------------------------------------------------------------------
myString = "abc"
stringLength = len(myString)
print stringLength
#------------------------------------------------------------------
string1 = "abra"
string2 = "cadabra"
magicString = string1 + string2
print magicString
#------------------------------------------------------------------
myNumber = "2"
print myNumber + myNumber
#------------------------------------------------------------------
myNumber = "12"
print myNumber * 3
#------------------------------------------------------------------
myNumber = "12"
print int(myNumber)
#------------------------------------------------------------------
phrase = "the surprise is in here somewhere"
print phrase.find("surprise") #== output 4
#------------------------------------------------------------------
myStory = "I'm telling you the truth; he spoke nothing but the truth!"
print myStory.replace("the truth", "lies") # I'm telling you lies; he spoke nothing but lies!
#------------------------------------------------------------------
counter = 100          # An integer assignment
miles   = 1000.0       # A floating point
name    = "John"       # A string
print (counter)
print (miles)
print (name)
#------------------------------------------------------------------
long_string = '''This is a
string that spans across multiple lines'''
long_string = """This is a new string
that spans across two lines"""
#------------------------------------------------------------------
string1 = "abra"
string2 = "cadabra"
magic_string = string1 + string2
print(magic_string)
#------------------------------------------------------------------
print("abra"+"ca"+"dabra") #abracadabra
print("abra", "ca", "dabra") # abra ca dabra
#------------------------------------------------------------------
flavor = "birthday cake"
print(flavor[3]) #t
print(flavor[0]) #b
#------------------------------------------------------------------
flavor = "birthday cake"
print(flavor[0:3]) # bir
#------------------------------------------------------------------
flavor = "birthday cake"
print(flavor[:5]) # birth
print(flavor[5:]) # day cake
print(flavor[:]) # birthday cake
#------------------------------------------------------------------
my_string = "abc"
string_length = len(my_string)
print(string_length) # 3
#------------------------------------------------------------------
name1 = "Jane"
age1 = 12
print name1, age1
#------------------------------------------------------------------
baskets = 16
apples_in_basket = 24
total = baskets * apples_in_basket
print "There are total of", total, "apples" #There are total of 384 apples
#------------------------------------------------------------------
distance = 0.1
time = 9.87 / 3600
speed = distance / time
print "The average speed of" \
      " a sprinter is " , speed, "km/h" #The average speed of a sprinter is  36.4741641337 km/h
#------------------------------------------------------------------
print "eagle " * 5 # eagle eagle eagle eagle eagle 
print "eagle " "falcon" # eagle falcon
print "eagle " + "and " + "falcon" # eagle and falcon
#------------------------------------------------------------------
first = (1, 2, 3)
second = (4, 5, 6)
print "len(first) : ", len(first) # len(first) :  3
print "max(first) : ", max(first) # max(first) :  3
print "min(first) : ", min(first) # min(first) :  1
print "first + second :", first + second # first + second : (1, 2, 3, 4, 5, 6)
print "first * 3 : ", first * 3 # first * 3 :  (1, 2, 3, 1, 2, 3, 1, 2, 3)
print "1 in first : ", 1 in first # 1 in first :  True
print "5 not in second : ", 5 not in second # 5 not in second :  False
#------------------------------------------------------------------
five = (1, 2, 3, 4, 5)
print "five[0] : ", five[0] # five[0] :  1
print "five[-1] : ", five[-1] # five[-1] :  5
print "five[-2] : ", five[-2] # five[-2] :  4
print "five[:] : ", five[:] # five[:] :  (1, 2, 3, 4, 5)
print "five[0:4] : ", five[0:4] # five[0:4] :  (1, 2, 3, 4)
print "five[1:2] : ", five[1:2] # five[1:2] :  (2,)
print "five[:2] : ", five[:2] # five[:2] :  (1, 2) 
print "five[:-1] : ", five[:-1] # five[:-1] :  (1, 2, 3, 4)
print "five[:9] : ", five[:9] # five[:9] :  (1, 2, 3, 4, 5)
#------------------------------------------------------------------
mix = (1, 2, "solaris", (1, 2, 3))
print "mix[1] :", mix[1] # mix[1] : 2
print "mix[2] :", mix[2] # mix[2] : solaris
print "mix[3] :", mix[3] # mix[3] : (1, 2, 3)
print "mix[3][0] :", mix[3][0] # mix[3][0] : 1
print "mix[3][1] :", mix[3][1] # mix[3][1] : 2
print "mix[3][2] :", mix[3][2] # mix[3][2] : 3
#------------------------------------------------------------------
s = " Eagle  "
s2 = s.rstrip()
s3 = s.lstrip()
s4 = s.strip()
print s, len(s) # Eagle   8
print s2, len(s2) # Eagle 6
print s3, len(s3) # Eagle   7
print s4, len(s4) # Eagle 5
#------------------------------------------------------------------
print "Incompatible, it don't matter though\n'cos someone's bound to hear my cry"
print "Speak out if you do\nYou're not easy to find"
#------------------------------------------------------------------
print "12" == "12" # True
print "17" == "9" # False
print "aa" == "ab" # False
print "abc" != "bce" # True
print "efg" != "efg" # False
#------------------------------------------------------------------
import math
pi = 3.14
print math.cos(3) # -0.9899924966
print math.pi # 3.14159265359
print math.sin(3) # 0.14112000806
print pi # 3.14

Related Post