214 lines
2.1 KiB
Plaintext

name = 'Fred'
# Using the old .format() method:
print('His name is {var}.'.format(var=name))
# Using f-strings:
print(f'His name is {name}.')
print(f'His name is {name!r}')
d = {'a':123,'b':456}
print(f'Address: {d['a']} Main Street')
d = {'a':123,'b':456}
print(f"Address: {d['a']} Main Street")
library = [('Author', 'Topic', 'Pages'), ('Twain', 'Rafting', 601), ('Feynman', 'Physics', 95), ('Hamilton', 'Mythology', 144)]
for book in library:
print(f'{book[0]:{10}} {book[1]:{8}} {book[2]:{7}}')
for book in library:
print(f'{book[0]:{10}} {book[1]:{10}} {book[2]:.>{7}}') # here .> was added
from datetime import datetime
today = datetime(year=2018, month=1, day=27)
print(f'{today:%B %d, %Y}')
%%writefile test.txt
Hello, this is a quick test file.
This is the second line of the file.
myfile = open('whoops.txt')
pwd
# Open the text.txt file we created earlier
my_file = open('test.txt')
my_file
# We can now read the file
my_file.read()
# But what happens if we try to read it again?
my_file.read()
# Seek to the start of file (index 0)
my_file.seek(0)
# Now read again
my_file.read()
# Readlines returns a list of the lines in the file
my_file.seek(0)
my_file.readlines()
my_file.close()
# Add a second argument to the function, 'w' which stands for write.
# Passing 'w+' lets us read and write to the file
my_file = open('test.txt','w+')
# Write to the file
my_file.write('This is a new first line')
# Read the file
my_file.seek(0)
my_file.read()
my_file.close() # always do this when you're done with a file
my_file = open('test.txt','a+')
my_file.write('\nThis line is being appended to test.txt')
my_file.write('\nAnd another line here.')
my_file.seek(0)
print(my_file.read())
my_file.close()
%%writefile -a test.txt
This is more text being appended to test.txt
And another line here.
with open('test.txt','r') as txt:
first_line = txt.readlines()[0]
print(first_line)
txt.read()
with open('test.txt','r') as txt:
for line in txt:
print(line, end='') # the end='' argument removes extra linebreaks