<jupyter_start><jupyter_text>2-1.3 Intro Python Sequence: String- Accessing String Character with index- Accessing sub-strings with index slicing- **Iterating through Characters of a String**- More String Methods----- > Student will be able to  - Work with String Characters- Slice strings into substrings- **Iterate through String Characters**- Use String Methods  &nbsp; Concepts Iterate a String: 1 character at a time[![view video](https://iajupyterprodblobs.blob.core.windows.net/imagecontainer/common/play_video.png)]( http://edxinteractivepage.blob.core.windows.net/edxpages/f7cff1a7-5601-48a1-95a6-fd1fdfabd20e.html?details=[{"src":"http://jupyternootbookwams.streaming.mediaservices.windows.net/edd3631b-bceb-45a1-82bb-addf532aba4d/Unit2_Section1.3a-Iterate_a_String.ism/manifest","type":"application/vnd.ms-sstr+xml"}],[{"src":"http://jupyternootbookwams.streaming.mediaservices.windows.net/edd3631b-bceb-45a1-82bb-addf532aba4d/Unit2_Section1.3a-Iterate_a_String.vtt","srclang":"en","kind":"subtitles","label":"english"}]) `for letter in word:`Python provides powerful sequence iteration features.  Below, **`for letter in word:`** loops through each letter in *word*.```pythonword = "cello"for letter in word:    print(letter)```  The variable **`letter`** is an arbitrary variable name .  Any valid variable name can be used.  &nbsp; Examples<jupyter_code># [ ] review and run example
word = "cello"

for letter in word:
    print(letter)
# [ ] review and run example
# note: the variable 'letter' changed to 'item'
word = "trumpet"

for item in word:
    print(item)
# [ ] review and run example
# note: variable is now 'xyz' 
# using 'xyz', 'item' or 'letter' are all the same result
word = "piano"

for xyz in word:
    print(xyz)
# [ ] review and run example
# creates a new string (new_name) adding a letter (ltr) each loop
# Q?: how many times will the loop run?
student_name = "Skye"
new_name = ""

for ltr in student_name:
    if ltr.lower() == "y":
        new_name += ltr.upper()
    else:
        new_name += ltr
print(student_name,"to",new_name)<jupyter_output>Skye to SkYe<jupyter_text>&nbsp; Task 1 iterate a String one character at a time<jupyter_code># [ ] Get user input for first_name
# [ ] iterate through letters in first_name 
#    - print each letter on a new line

first_name = input ("Enter a first name: ")

for letter in first_name:
    print(letter)<jupyter_output>Enter a first name: Thomas
T
h
o
m
a
s<jupyter_text>&nbsp; Task 2 Program: capitalize-io- get user input for first_name- create an empty string variable: new_name- iterate through letters in first_name   -  add each letter in new_name  - capitalize if letter is an "i" or "o" *(hint: if, elif, else)- print new_name<jupyter_code># [ ] Create capitalize-io
first_name = input("Enter a first name: ")
new_name = ""

for letter in first_name:
    if letter.lower() == "i":
        new_name += letter.upper()
    elif letter.lower() == "o":
        new_name += letter.upper()
    else:
        new_name += letter
        
print (new_name)<jupyter_output>Enter a first name: Iisaah
IIsaah<jupyter_text>&nbsp; Concepts Iterate sub-strings [![view video](https://iajupyterprodblobs.blob.core.windows.net/imagecontainer/common/play_video.png)]( http://edxinteractivepage.blob.core.windows.net/edxpages/f7cff1a7-5601-48a1-95a6-fd1fdfabd20e.html?details=[{"src":"http://jupyternootbookwams.streaming.mediaservices.windows.net/257ed101-c530-406a-ba20-6d437d88e529/Unit2_Section1.3b-Iterate-Substrings.ism/manifest","type":"application/vnd.ms-sstr+xml"}],[{"src":"http://jupyternootbookwams.streaming.mediaservices.windows.net/257ed101-c530-406a-ba20-6d437d88e529/Unit2_Section1.3b-Iterate-Substrings.vtt","srclang":"en","kind":"subtitles","label":"english"}])Combine String slicing and iteration```pythonstudent_name = "Skye"for letter in student_name[:3]:    print(letter)```  Iterate backwards using: **`student_name[::-1]`**  &nbsp; Example<jupyter_code># [ ] review and run example
student_name = "Skye"

for letter in student_name[:3]:
    print(letter)
# Iterate BACKWARDS
# [ ] review and run example
student_name = "Skye"

# start at "y" (student_name[2]), iterate backwards
for letter in student_name[2::-1]:
    print(letter)<jupyter_output>y
k
S<jupyter_text>&nbsp; Task 3  String slicing and iteration<jupyter_code># [ ] create & print a variable, other_word, made of every other letter in long_word
long_word = "juxtaposition"
other_word = long_word[::2]
print (other_word)

# Mirror Color
# [ ] get user input, fav_color
# [ ] print fav_color backwards + fav_color
# example: "Red" prints "deRRed"

fav_color = input("Enter your favorite color: ")

print (fav_color[::-1] + fav_color)<jupyter_output>Enter your favorite color: Purple
elpruPPurple
{'fraction_non_alphanumeric': 0.13044393014937933, 'fraction_numerical': 0.027561540079949504, 'mean_word_length': 5.102695763799743, 'pattern_counts': {'":': 15, '<': 16, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '19425', 'license': 'MIT', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1634, 'n_words': 417, 'path': 'Python Fundamentals/Module_1_3_Python_Fundamentals.ipynb', 'repo_name': 'sdavi187/pythonteachingcode', 'size': '4753'}