LoslegenKostenlos loslegen

Manipulating strings with methods

Methods are special functions that are attached to objects. Certain data types have methods that are convenient for manipulating the data. For example, the .upper() method manipulates string variables by returning the uppercase version of a string. The .lower() method returns the lowercase version of the string.

city = 'New York'
print(city.lower())

new york

Another very useful method for strings is .format(), which replaces any curly brackets in a string with the arguments passed into it.

city = 'New York'
print("City: {}".format(city))

City: New York

Practice manipulating string variables using methods while learning a fun American history fact!

Diese Übung ist Teil des Kurses

Python for MATLAB Users

Kurs anzeigen

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# Print the lowercase version of this Jeopardy clue
clue = 'Signer of the Dec. of Indep., framer of the Constitution of Mass., second President of the United States'

print(clue.____)
Code bearbeiten und ausführen