'''lecture_23_strings.py
Demonstrate string properties. Make sure that you understand what everything does.
Oliver W. Layton
CS 151: Computational Thinking: Visual Media
Fall 2020
'''

'''Escape sequences'''
# New line: \n
# Example: Print a string variable with new lines.
#
# Text: 'Haikus are easy. But sometimes they dont make sense. Refrigerator.'
# haiku = 'Haikus are easy.\nBut sometimes they dont make sense.\nRefrigerator.'
# print(haiku)

# Show similarity to multiple print statments, which insert new line escape sequence behind-the-scenes


# Tabs: \t
# haiku = 'Haikus are easy.\tBut sometimes they dont make sense.\tRefrigerator.'
# haiku = '\t\tHaikus are easy.\n\t\tBut sometimes they dont make sense.\n\t\tRefrigerator.'
# print(haiku)

# Apostrophes or quotes in strings: \' or \"
# does not work:
# haiku = 'Haikus are easy. But sometimes they don't make sense. Refrigerator.'
# Example: Missing quote in haiku
# haiku = 'Haikus are easy. But sometimes they don\'t make sense. Refrigerator.'
# print(haiku)

# print(haiku)
# Show how mixed quotation symbols also work (recent versions of Python only)
# haiku = "Haikus are easy. But sometimes they don't make sense. Refrigerator."
# print(haiku)

# interview = "The person said \"hi\""
# print(interview)

# How to type backlash?
# print('Here is a backslash: \')  # does not work: Python thinks escape sequence is incomplete
# myString = 'Here is a backslash: \\'
# print(myString)  # you mean backslash character, not escape sequence
# No variable:
# print('Here is a backslash: \\')  # you mean backslash character, not escape sequence

'''Length'''
# print(len('haiku'))

'''String operators'''
# Concatenation: + operator with two strings
one = 'bat'
two = 'man'
three = one + two
# print(three)


# Replication: * operator with one string and one int
# one = 'bat'
# print(one*10)


# Accessing single characters
one = 'bat'
print('First letter is', one[0])
print('Last letter is', one[-1])

# Loop thru a string like a list of characters:
for letter in 'batman':
    print(letter)


# Accessing ranges of characters (substrings)
# We can slice strings like lists, but we can't use assignment to replace characters or substrings
# Let's access 'bat' in 'batman'
print('First word in batman:', three[0:3])
print('Last word in batman:', three[-3:])


'''Substrings'''
# Determine whether a string (the subtring) appears / doesn't appear inside another one
# Text: 'two wrongs don\'t make a right'
# Text: 'right'
# a = 'two wrongs don\'t make a right'
# b = 'right'
# print('Is "right" in the bigger string?', b in a)
# print('Is "right" not in the bigger string?', b not in a)


'''Iterating through strings'''
# We can iterate thru strings, not just range and lists:
# Text: 'Hello World'
# myString = 'Hello World'
# for letter in myString:
#     print(letter)


'''Reading text from a file'''
# Let's read in text from the famous book Frankenstein stored in a text file as a big string
# 3 primary ways to do this
# 1) Read whole file in at once as a single string
file = open('frankensteinChaper8.txt', 'r')
theText = file.read()
file.close()
print(theText)


# 2) Read individual lines until we hit the end of the file (indicated by empty string)
# file = open('frankensteinChaper8.txt', 'r')
# frankText = ''


# 3) "Looping" thru a file
# file = open('frankensteinChaper8.txt', 'r')
# frankText = ''


'''Important string methods'''
# Strings are technically objects and have methods. There are many:
# https://docs.python.org/3/library/stdtypes.html#string-methods)
# Here are important ones that you should know (minimally):
# upper, lower, title, split(char), join, find(word), replace, count
# isalpha, isdigit, isupper, isalnum
# Example: Let's see some string methods in action.
# print(frankText)
# print(frankText.upper())
# print(frankText.lower())
# print(frankText.title())

# NOTE: split gives us a LIST
# Example extract words/sentences


# Join: Opposite of split. Takes in list and converts it to a string, seperated by the string it is called on.
# Text: 'two wrongs don\'t make a right'
# asString = 'two wrongs don\'t make a right'


# Join can be useful if you decide to read from a file line-by-line and store each line as an entry
# in a list. It can convert the list of line strings to one big string, like we've had above:
# file = open('frankensteinChaper8.txt', 'r')


# Find: Example, find worked 'passed'. Print it out.
# if the search string is not found, find returns -1


# Replace: Example, replace word 'We' with 'I'. Print it out.


# Count: Find total number of Is in chapter
# print(frankText.count('I'))

'''Read in data from files'''
# Let's do a few examples in `lecture_23_while_file.py`
# 1. Reading data from files: Read one number per line in the file `data.txt`
# 2. Reading data from files (combining while and for loops): Read multiple numbers per line in file `data.csv`

'''Write data to a file'''
# 3. Write statistics to a file
