3. Concatenation

3.1. A string cannot be modified

3.2. I can create new strings

  • Using concatenation solves this problem

System Message: ERROR/3 (/home/runner/work/PyZombis/PyZombis/_sources/lectures/TWP18/TWP18_3_en.rst, line 20)

Duplicate ID – see lectures/TWP18/TWP18_3_en, line 11

.. activecode:: python
    :nocodelens:
    :stdin:

    texto = "Hello world"
    texto = "@" + texto[1:]
    print(texto)

  • Example of a program that reads a word and replaces the vowels with "*". The lower function transforms the letters to lowercase.

System Message: ERROR/3 (/home/runner/work/PyZombis/PyZombis/_sources/lectures/TWP18/TWP18_3_en.rst, line 33)

Duplicate ID – see lectures/TWP18/TWP18_3_en, line 11

.. activecode:: python
    :nocodelens:
    :stdin:

    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)

3.3. Exercise

System Message: ERROR/3 (/home/runner/work/PyZombis/PyZombis/_sources/lectures/TWP18/TWP18_3_en.rst, line 52)

Duplicate ID – see lectures/TWP18/TWP18_3_en, line 11

.. activecode:: python
    :nocodelens:
    :stdin:

    # 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.

    ~~~~
    # Use the input function to read the word from the user.


    ====
    from unittest.gui import TestCaseGui


    class myTests(TestCaseGui):
        def testOne(self):
            self.assertEqual(
                intercambio,
                "".join(["*" if c.lower() not in "aeiou" else c for c in palabra]),
                "Testing that intercambio is correctly assigned",
            )


    myTests().main()
You have attempted of activities on this page