Studying Python 2.7.
Increment is ʻi = i + 1`
There is no loop like for (i = 0; i <10; i ++)
like in other languages
Like using while
cnt = 0
while cnt<10:
print cnt
cnt += 1
Make a list of the specified range with the range function and turn it with for
for i in range(10):
print i
Variables are defined by the type defined first.
Numerical values are converted to strings and then concatenated
print 'abc'+str(123)
Or use format
print 'abc{0}'.format(123)
print len('AIUEO')
# 15
print len(u'AIUEO')
print len('AIUEO'.decode('utf-8'))
print len(unicode('AIUEO', 'utf-8'))
#All the above results are 5
Something like sprintf
'%s %d %f' % ('AIUEO', 10, 1.2345)
format function
print '{0} {1} {2}'.format('AIUEO', 10, 1.2345)
Sprintf-like guys using% can use the same flags as sprintf
print '%s %04d %.2f' % ('AIUEO', 10, 1.2345)
#Aiueo 0010 1.23
The following code will result in an error
print '{0} {1} {2}'.format(u'String', 10, 1.5)
#UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
The correct answer is that the format string is also a unicode string
print u'{0} {1} {2}'.format(u'String', 10, 1.5)
#String 10 1.5
Recommended Posts