2013-12-29 22:34:42 +00:00
|
|
|
# basic strings
|
|
|
|
|
2015-04-04 23:16:22 +01: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 21:20:58 +00:00
|
|
|
print(str())
|
2015-04-04 23:16:22 +01:00
|
|
|
print(str('abc'))
|
2015-03-14 21:20:58 +00:00
|
|
|
|
2015-04-04 23:16:22 +01:00
|
|
|
# inplace addition
|
2013-12-29 22:34:42 +00:00
|
|
|
x = 'abc'
|
|
|
|
print(x)
|
|
|
|
x += 'def'
|
|
|
|
print(x)
|
|
|
|
|
2015-04-04 23:16:22 +01:00
|
|
|
# binary ops
|
2013-12-29 22:34:42 +00:00
|
|
|
print('123' + "456")
|
2014-01-20 22:27:33 +00:00
|
|
|
print('123' * 5)
|
2015-08-21 11:56:14 +01:00
|
|
|
try:
|
|
|
|
'123' * '1'
|
|
|
|
except TypeError:
|
|
|
|
print('TypeError')
|
2015-09-03 23:06:18 +01:00
|
|
|
try:
|
|
|
|
'123' + 1
|
|
|
|
except TypeError:
|
|
|
|
print('TypeError')
|
2014-01-20 22:27:33 +00:00
|
|
|
|
2015-04-04 23:16:22 +01:00
|
|
|
# subscription
|
2014-01-21 00:22:02 +00:00
|
|
|
print('abc'[1])
|
|
|
|
print('abc'[-1])
|
|
|
|
try:
|
|
|
|
'abc'[100]
|
|
|
|
except IndexError:
|
2015-08-21 11:56:14 +01:00
|
|
|
print('IndexError')
|
2014-01-21 00:22:02 +00:00
|
|
|
try:
|
|
|
|
'abc'[-4]
|
|
|
|
except IndexError:
|
2015-08-21 11:56:14 +01:00
|
|
|
print('IndexError')
|
2014-01-21 00:22:02 +00:00
|
|
|
|
2014-01-05 10:47:51 +00:00
|
|
|
# iter
|
|
|
|
print(list('str'))
|
2014-01-08 20:41:22 +00:00
|
|
|
|
2015-04-04 22:05:30 +01:00
|
|
|
# comparison
|
2014-01-08 20:41:22 +00:00
|
|
|
print('123' + '789' == '123789')
|
|
|
|
print('a' + 'b' != 'a' + 'b ')
|
2015-04-04 22:05:30 +01:00
|
|
|
print('1' + '2' > '2')
|
|
|
|
print('1' + '2' < '2')
|
2014-01-08 20:41:22 +00:00
|
|
|
|
2015-04-04 22:05:30 +01:00
|
|
|
# printing quote char in string
|
|
|
|
print(repr('\'\"'))
|