Skip to content

Instantly share code, notes, and snippets.

@dketov
Created December 11, 2011 19:39
Show Gist options
  • Save dketov/1462316 to your computer and use it in GitHub Desktop.
Save dketov/1462316 to your computer and use it in GitHub Desktop.
Строка, часть 2
# -*- encoding: utf-8 -*-
"""
Вывод строки
"""
i = 256 * 256
print 'The value of i is', i
# -*- encoding: utf-8 -*-
"""
Подстановка
"""
S = 'spammy'
S = S.replace('mm', 'xx')
print S
print 'aa$bb$cc$dd'.replace('$', 'SPAM')
# -*- encoding: utf-8 -*-
"""
Разбиение
"""
string1 = "A, B, C, D, E, F"
print "String is:", string1
print "Split string by spaces:", string1.split()
print "Split string by commas:", string1.split( "," )
print "Split string by commas, max 2:", string1.split( ",", 2 )
print
# -*- encoding: utf-8 -*-
"""
Объединение
"""
print 'SPAM'.join(['eggs', 'sausage', 'ham', 'toast'])
# -*- encoding: utf-8 -*-
"""
Форматирование
"""
print "%2s %5s %12s" % ('x', 'x**2', 'x**x')
print "=" * 21
for x in range(1,6):
print "%2d %5d %12d" % (x, x**2, x**x)
# -*- encoding: utf-8 -*-
"""
Объединение с форматированием
"""
list1 = [ "A", "B", "C", "D", "E", "F" ]
string2 = "___"
print "List is:", list1
print 'Joining with "%s": %s' \
% ( string2, string2.join ( list1 ) )
print 'Joining with "-.-":', "-.-".join( list1 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment