'''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('Number of characters is', len(theText))
# print(theText[-40:])


# 2) Read individual lines until we hit the end of the file (indicated by empty string)
# file = open('frankensteinChaper8.txt', 'r')
# frankText = ''
# oneLine = file.readline()  # reads line 1
# print(oneLine)
# oneLine = file.readline()  # reads line 2
# print(oneLine)
# oneLine = file.readline()  # reads line 3
# line1 = file.readline()  # reads line 1
# line2 = file.readline()  # reads line 2
# line3 = file.readline()  # reads line 3
# first3Lines = line1 + line2 + line3
# print(first3Lines)
# file.readline()  # this will read line 4
# print('a', end='\n')
# print('b')
# while oneLine != '':
#     frankText = frankText + oneLine
#     oneLine = file.readline()
# print(frankText)
# print('Last line of paragraph', '\n', '\nSome more text')


# 3) "Looping" thru a file
# file = open('frankensteinChaper8.txt', 'r')
# frankText = ''
# for line in file:
#     # print('One line:', line, end='')
#     frankText = frankText + line
# print(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
# sentList = frankText.split('.')
# # print(sentList)
# print(sentList[0])  # 1st sentence
# print(sentList[1])  # 2nd sentence

# wordList = frankText.split(' ')
# print(wordList)
# print(wordList[1])

# 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'
# asList = asString.split(' ')
# print(asList)
# backAsString = ' '.join(asList)
# print(backAsString)  # string not a list

# print(' never '.join(asList))
# print(' ---- '.join(asList))
# print(' !!!! '.join(asList))
# print('\n'.join(asList))
# print('\t'.join(asList))

# 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')
# frankText = []
# for line in file:
#     frankText.append(line)
# file.close()
# # print(frankText)
# frankText = ''.join(frankText)


# Find: Example, find worked 'passed'. Print it out.
# if the search string is not found, find returns -1
# print('Index of passed:', frankText.find('passed'))
# print('Index of passed:', frankText.find('99999'))
# print(frankText[:40])


# Replace: Example, replace word 'We' with 'I'. Print it out.
# print(frankText)
# print(frankText.replace('the', 'XXXXXXXX'))

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

'''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
