Lab 6: Strings with For Loops

Handling strings

A necklace strung with 5 beads. Each bead has a letter on it, and the letters spell out C S 1 1 1.

Table of some string operations:

symbol operation example
+ concatenation str1 = 'I am '
str2 = 'your father '
darth = str1 + str2
yoda = str2 + str1
* repetition 'hee'*3 ==> 'heeheehee'
[] index str2[5]==> 'f'
[:] slice str2[2:5]==> 'ur '
in in 'dad' in str2 ==> False
'at' in str2 ==> True
not in not in str1 not in str2 ==> True
str1 not in darth ==> False

A couple of handy string tips:

String functions with loops

Create a file named lab06.py for this part of the lab.

betterStarify

Write a function called betterStarify(word) that takes in a word, and returns a new string with a * after each letter in the original word, but not after the last letter in the word.

print(betterStarify('OMG')) # should print O*M*G
print(betterStarify('wicked')) # should print w*i*c*k*e*d
print(betterStarify('Starry')) # should print S*t*a*r*r*y

hasDoubleVowel

Write a predicate called hasDoubleVowel that takes a word as its only parameter, and returns True if the word has two of the same vowel in a row, or False if it doesn't. For example, it should return True for eel and pool but False for feather and tell ('feather' has two vowels in a row, but they're not the same vowel; 'tell' has two of the same letter in a row, but they're not vowels).

Feeling lost? Click here for a hint!

For this problem, an index loop is helpful, since it allows you to refer to a previous or upcoming letter in addition to the letter at the current index within the loop (just subtract or add 1 to the index). Just be careful to either start at index 1 or stop before the normal final index so that you don't try to access a letter before the beginning or after the end of the word.

starryRow and starrySky

In the box below, you're given a predicate called starTime that returns True twenty percent of the time and False eighty percent of the time.


You can copy/paste the starTime predicate below into your file (and add the import while you're at it):

import random

def starTime():
    """returns True 20% of the time; False otherwise.
    Note: random.random() returns a random number between 0 and 1
    """
    return random.random() > 0.80

Write a fruitful function called starryRow that takes one parameter rowLength, and returns a string with length rowLength that is made up of stars (*) and dashes (-).

When generating the row, you can use the starTime predicate so that 20% of the row is stars (on average). For each spot in the row, use the predicate starTime: if it returns True, add a star to your row, otherwise, add a dash. Here are some sample invocations of starryRow:

print(starryRow(20)) # might print -*------*--*----**--
print(starryRow(20)) # might print ----*----*----------
print(starryRow(20)) # might print ---***-*--**------*-
print(starryRow(20)) # might print *--**----*--**--*---

Now write another fruitful function called starrySky that takes two parameters: width and height. starrySky returns a string that contains a 'sky' that has width and height dimensions and each row is created by a call to starryRow. The sky is built by adding each row and a newline character (\n) at the end. Here are some sample invocations along with what their output might look like:

print(starrySky(10, 10))
*-------*-
--*-------
----------
---**-----
**--*--*--
--------*-
----------
-----*----
------*---
--*-------
print(starrySky(5, 50))
----------------**-----**-*----------*---------*--
*--*----*---*---*-----**------*------------*--*---
-*---*---*--------*----------**--------*---*--*---
---**----*-**---------**---*-------**--*-------*--
------**-----------------*--------*-----*---*--*--
print(starrySky(3, 60))
--------*-**-**---*---*---------*--*--**-------------------*
----*---------*--*-*--*-**---*----*--------*---*-*----*----*
----*---------*--*-*--*-**---*----*--------*---*-*----*----*

funnyVoice

Write a function called funnyVoice that takes in a sentence as a parameter, and returns a new sentence, based on the original sentence, with each letter randomly capitalized (with a 50% chance of being capitalized. You will need to use the random.randint and/or random.random functions. Use those links to read our quick-reference documentation for a refresher if you need to.

Here are the general python documentation pages for random numbers if you're curious about more details.

print(funnyVoice("Hello"))
# might print: heLlO
print(funnyVoice("SpongeBob SquarePants"))
# might print: sPOnGeBOB sqUArepANtS

Table of Contents