In [1]:
# this is an empty string
empty_str = ''
In [2]:
# create a string
str1 = ' the quick brown fox jumps over the lazy dog. '
str1
Out[2]:
In [3]:
# strip whitespaces from the beginning and ending of the string
str2=str1.strip()
print str2
print str1
In [4]:
# this capitalizes the 1st letter of the string
str2.capitalize()
Out[4]:
In [6]:
# count the number of occurrences for the string o
str2.count('o')
Out[6]:
In [7]:
# check if a string ends with a certain character
str2.endswith('.')
Out[7]:
In [10]:
# check if a substring exists in the string
'jum' in str2
Out[10]:
In [9]:
# find the index of the first occurrence
str2.find('fox')
Out[9]:
In [11]:
# let's see what character is at index 19
str2[19]
Out[11]:
Note: strings are immutable while lists are not. In other words immutability does not allow for in-place modification of the object.
In [12]:
S = 'shrubbery'
S[1]='c'
In [13]:
S = 'shrubbery'
print "length of string is : ", len(S)
L = list(S)
print "L = ", L
In [14]:
S[0]
Out[14]:
In [15]:
L[1] = 'c'
print L
S1 = ''.join(L)
print 'this is S1:', S1
print S
In [16]:
for x in S:
print x
In [17]:
# another way of changing the string
S = S[0] + 'c' + S[2:] # string concatenation
S
Out[17]:
In [18]:
#
line = 'aaa,bbb,cccc c,dd'
line1 = line.split(',')
print line
print line1
In [21]:
# list the methods and attributes for string operations
dir(S)
Out[21]:
In [20]:
help(S.split)
In [ ]:
ord(S[0])
In [22]:
# define variables
x = 3.1415926
y = 1
In [23]:
# 2 decimal places
print "{:.2f}".format(x)
In [24]:
# 2 decimal palces with sign
print "{:+.2f}".format(x)
In [25]:
# 2 decimal palces with sign
print "{:.2f}".format(-y)
In [26]:
# print with no decimal palces
print "{:.0f}".format(3.51)
In [27]:
# left padded with 0's - width 4
print "{:0>4d}".format(11)
In [28]:
for i in range(20):
print "{:0>4d}".format(i)
In [29]:
# right padd with x's - total width 4
print "{:x<4d}".format(33)
In [30]:
# right padd with x's - total width 4
print y
print "{:x<4d}".format(10*y)
In [31]:
# insert a comma separator
print "{:,}".format(10000000000000)
In [32]:
# % format
print "{:.4%}".format(0.1235676)
In [33]:
# exponent notation
print "{:.3e}".format(10000000000000)
In [34]:
# right justified, with 10
print '1234567890' # for place holders
print "{:10d}".format(10000000)
In [35]:
# left justified, with 10
print '12345678901234567890' # place holder
print "{:<10d}".format(100), "{:<10d}".format(100)
In [36]:
# center justified, with 10
print '1234567890'
print "{:^10d}".format(100)
In [37]:
# string substitution
s1 = 'so much depends upon {}'.format('a red wheel barrow')
s2 = 'glazed with {} water beside the {} chickens'.format('rain', 'white')
print s1
print s2
In [38]:
# another substitution
s1 = " {0} is better than {1} ".format("emacs", "vim")
s2 = " {1} is better than {0} ".format("emacs", "vim")
print s1
print s2
In [39]:
## defining formats
email_f = "Your email address was {email}".format
print email_f
## use elsewhere
var1 = "bob@example.com"
var2 = 'a@cox.net'
var3 = 'b@cox.net'
print(email_f(email=var1))
print(email_f(email=var2))
print(email_f(email=var3))