Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vijayanandrp/30fc0162e36ab507709ba1f8e347987c to your computer and use it in GitHub Desktop.
Save vijayanandrp/30fc0162e36ab507709ba1f8e347987c to your computer and use it in GitHub Desktop.
How to do I understand strings Python concepts in just 5 mins!!
#!/usr/bin/env python
def main():
print int('5')
print ord('s')
print chr(115)
print bin(13)
print hex(26)
print hex(255)
print oct(255)
print bin(255)
print int('377', 8), int('FF', 16)
print str('Vijay')
print repr('Vijay') # Removed in 3.X python
line = "The knights who say N1!\n"
print line.endswith('\n')
print line.startswith('The'), '\n'
print '%(qty)d more %(food)s' % {'qty': 1, 'food': 'Dosai'}, '\n'
# Method 1 of formatting output
values = {}
values = dict(name='Vijay', job='testing', age=24)
print 'Hi Guys, My name is %(name)s and age is %(age)d. My Current job is %(job)s' % values, '\n'
# Method 2 of formatting
template = '{0}, {1} and {2}'
print template.format('spam', 'ham', 'eggs')
template = '{}, {} and {}'
print template.format('spam', 'ham', 'eggs')
template = '{motto}, {pork} and {food}'
print template.format(motto='spam', pork='ham', food='eggs')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment