# [ ] review and run example
tip = "Notebooks can be exported as .pdf"
tip_words = tip.split()
print("STRING:", tip)
print("LIST:", tip_words, "\n")
for word in tip_words:
print(word)
STRING: Notebooks can be exported as .pdf
LIST: ['Notebooks', 'can', 'be', 'exported', 'as', '.pdf']
Notebooks
can
be
exported
as
.pdf
# [ ] review and run example
rhyme = "London bridge is falling down"
rhyme_words = rhyme.split()
rhyme_words.reverse()
for word in rhyme_words:
print(word)
down
falling
is
bridge
London
# [ ] split the string(rhyme) into a list of words (rhyme_words)
# [ ] print each word on it's own line
rhyme = 'Jack and Jill went up the hill To fetch a pail of water'
rhyme_words = rhyme.split()
for word in rhyme_words:
print(word)
Jack
and
Jill
went
up
the
hill
To
fetch
a
pail
of
water
# [ ] split code_tip into a list and print the first and every other word
code_tip = "Python uses spaces for indentation"
code_tip_list = code_tip.split()
print(code_tip_list[0:5:2])
['Python', 'spaces', 'indentation']
# [ ] review and run example
code_tip = "Python-uses-spaces-for-indentation"
tip_words = code_tip.split('-')
print(tip_words)
['Python', 'uses', 'spaces', 'for', 'indentation']
# [ ] review and run example - study the list print output
code_tip = "Python uses spaces for indentation"
# split on "a"
tip_words = code_tip.split('a')
print(code_tip)
print(tip_words)
Python uses spaces for indentation
['Python uses sp', 'ces for indent', 'tion']
# [ ] review and run example
# triple quotes ''' ''' preserve formatting such as spaces and line breaks
big_quote = """Jack and Jill went up the hill
To fetch a pail of water
Jack fell down and broke his crown
And Jill came tumbling after"""
# split on line breaks (\n)
quote_lines = big_quote.split('\n')
print(quote_lines, '\n')
# print the list in reverse with index slicing
for line in quote_lines[::-1]:
print(line)
['Jack and Jill went up the hill', 'To fetch a pail of water', 'Jack fell down and broke his crown', 'And Jill came tumbling after']
And Jill came tumbling after
Jack fell down and broke his crown
To fetch a pail of water
Jack and Jill went up the hill
# [ ] split poem into a list of phrases by splitting on "*"
# [ ] print each phrase on a new line in title case
poem = "Write code frequently*Save code frequently*Comment code frequently*Study code frequently*"
split_poem = poem.split("*")
for word in split_poem:
print(word.title())
Write Code Frequently
Save Code Frequently
Comment Code Frequently
Study Code Frequently
# [ ] review and run example
tip_words = ['Notebooks', 'can', 'be', 'exported', 'as', '.pdf']
# join tip_words objects with spaces
print(" ".join(tip_words))
Notebooks can be exported as .pdf
# [ ] review and run example
no_space = ""
letters = ["P", "y", "t", "h", "o", "n"]
print(no_space.join(letters))
Python
# [ ] review and run example - .join() iterates through sequences
dash = "-"
space = " "
word = "Iteration"
ellipises = "..."
dash_join = dash.join(word)
print(dash_join)
print(space.join(word))
print(ellipises.join(word))
I-t-e-r-a-t-i-o-n
I t e r a t i o n
I...t...e...r...a...t...i...o...n
# [ ] .join() letters list objects with an Asterisk: "*"
letters = ["A", "s", "t", "e", "r", "i", "s", "k"]
join_letters = "*".join(letters)
print(join_letters)
A*s*t*e*r*i*s*k
# [ ] complete Choose the separator
phrase_words = ['Jack', 'and', 'Jill', 'went', 'up', 'the', 'hill', 'To', 'fetch', 'a', 'pail', 'of', 'water']
seperator = input("what do you want to use to join words: ")
phrase_words_together = seperator.join(phrase_words)
print(phrase_words_together)
Jack-and-Jill-went-up-the-hill-To-fetch-a-pail-of-water
# [ ] review and run example
hello_letters = list("Hello")
print(hello_letters)
['H', 'e', 'l', 'l', 'o']
# [ ] review and run example
# cast sting to list
word_letters = list("concatenates")
# .join() concatenates the list
# print on same line setting the end character
print('~'.join(word_letters))
c~o~n~c~a~t~e~n~a~t~e~s
# [ ] review and run example
print("Hello ", end = '')
print("world")
Hello world
# [ ] review and run example
# This is the default print end
print("Hello World!", end="\n")
print('still something to learn about print()')
Hello World!
still something to learn about print()
# [ ] review and run example
# end inserts any valid str character: A-z, 0-9,!,@,*,\n,\t or ''(empty string)...
for letter in "Concatenation":
print(letter, end='*')
C*o*n*c*a*t*e*n*a*t*i*o*n*
# [ ] use 3 print() statements to output text to one line
# [ ] separate the lines by using "- " (dash space)
print('Be strong and couragous.', end='- ')
print('For the Lord is with you', end='- ')
print('wherever you go.', end='- ')
Be strong and couragous.- For the Lord is with you- wherever you go.-
# [ ] create a string (fact) of 20 or more characters and cast to a list (fact_letters)
# [ ] iterate fact, printing each char on one line, except for spaces print a new line
fact = "All humans have two sections of a brain"
fact_letters = list("All humans have two sections of a brain")
for letters in fact:
if letters == " ":
print()
else:
print(letters, end='')
All
humans
have
two
sections
of
a
brain
# [ ] create add the digits
str_value = "12345678987654321234"
str_list = list("12345678987654321234")
sum = 0
for num in str_list:
if num.isdigit():
sum += int(num)
else:
print("string is not all digits")
print("+".join(str_list), "=", sum)
1+2+3+4+5+6+7+8+9+8+7+6+5+4+3+2+1+2+3+4 = 90