2013-12-29 17:34:42 -05:00
|
|
|
# basic strings
|
|
|
|
|
2015-04-04 18:16:22 -04:00
|
|
|
# literals
|
|
|
|
print('abc')
|
|
|
|
print(r'abc')
|
|
|
|
print(u'abc')
|
|
|
|
print(repr('\a\b\t\n\v\f\r'))
|
|
|
|
print('\z') # unrecognised escape char
|
|
|
|
|
|
|
|
# construction
|
2015-03-14 17:20:58 -04:00
|
|
|
print(str())
|
2015-04-04 18:16:22 -04:00
|
|
|
print(str('abc'))
|
2015-03-14 17:20:58 -04:00
|
|
|
|
2015-04-04 18:16:22 -04:00
|
|
|
# inplace addition
|
2013-12-29 17:34:42 -05:00
|
|
|
x = 'abc'
|
|
|
|
print(x)
|
|
|
|
x += 'def'
|
|
|
|
print(x)
|
|
|
|
|
2015-04-04 18:16:22 -04:00
|
|
|
# binary ops
|
2013-12-29 17:34:42 -05:00
|
|
|
print('123' + "456")
|
2014-01-20 17:27:33 -05:00
|
|
|
print('123' * 5)
|
2015-08-21 06:56:14 -04:00
|
|
|
try:
|
|
|
|
'123' * '1'
|
|
|
|
except TypeError:
|
|
|
|
print('TypeError')
|
2015-09-03 18:06:18 -04:00
|
|
|
try:
|
|
|
|
'123' + 1
|
|
|
|
except TypeError:
|
|
|
|
print('TypeError')
|
2014-01-20 17:27:33 -05:00
|
|
|
|
2015-04-04 18:16:22 -04:00
|
|
|
# subscription
|
2014-01-20 19:22:02 -05:00
|
|
|
print('abc'[1])
|
|
|
|
print('abc'[-1])
|
|
|
|
try:
|
|
|
|
'abc'[100]
|
|
|
|
except IndexError:
|
2015-08-21 06:56:14 -04:00
|
|
|
print('IndexError')
|
2014-01-20 19:22:02 -05:00
|
|
|
try:
|
|
|
|
'abc'[-4]
|
|
|
|
except IndexError:
|
2015-08-21 06:56:14 -04:00
|
|
|
print('IndexError')
|
2014-01-20 19:22:02 -05:00
|
|
|
|
2014-01-05 05:47:51 -05:00
|
|
|
# iter
|
|
|
|
print(list('str'))
|
2014-01-08 15:41:22 -05:00
|
|
|
|
2015-04-04 17:05:30 -04:00
|
|
|
# comparison
|
2014-01-08 15:41:22 -05:00
|
|
|
print('123' + '789' == '123789')
|
|
|
|
print('a' + 'b' != 'a' + 'b ')
|
2015-04-04 17:05:30 -04:00
|
|
|
print('1' + '2' > '2')
|
|
|
|
print('1' + '2' < '2')
|
2014-01-08 15:41:22 -05:00
|
|
|
|
2015-04-04 17:05:30 -04:00
|
|
|
# printing quote char in string
|
|
|
|
print(repr('\'\"'))
|