3. Concatenation¶
3.1. A string cannot be modified¶
3
texto = " Hello world!"
texto[0] = "@"
Activity: 3.1.1 ActiveCode (ac_l18_3a_en)
3.2. I can create new strings¶
Using concatenation solves this problem
4
texto = "Hello world"
texto = "@" + texto[1:]
print(texto)
Activity: 3.2.1 ActiveCode (ac_l18_3b_en)
Example of a program that reads a word and replaces the vowels with
"*"
. Thelower
function transforms the letters to lowercase.
11
palabra = input("Word: ")
k = 0
intercambio = ""
while k < len(palabra):
if palabra[k].lower() in "aeiou":
intercambio = intercambio + "*"
else:
intercambio = intercambio + palabra[k]
k += 1
print("New word %s" % intercambio)
Activity: 3.2.2 ActiveCode (ac_l18_3c_en)
3.3. Exercise¶
# Now create a program that reads a word, saves it in the variable “palabra”, # and replaces the consonants with “*”. Save the result in the variable “intercambio”. # You can use the previous program as a reference.
4
# Use the input function to read the word from the user.
Activity: 3.3.1 ActiveCode (ac_l18_3d_en)
You have attempted 1 of 5 activities on this page