Strings & Console Output
Lesson 1 of 2
  1. 1
    Another useful data type is the string. A string can contain letters, numbers, and symbols. name = “Ryan” age = “19” food = “cheese” 1. In the above example, we create a variable name an…
  2. 2
    Excellent! Let’s get a little practice in with strings.
  3. 3
    There are some characters that cause problems. For example: ‘There’s a snake in my boot!’ This code breaks because Python thinks the apostrophe in ‘There’s’ ends the string. We can use the backs…
  4. 4
    Great work! Each character in a string is assigned a number. This number is called the index. Check out the diagram in the editor. c = “cats”[0] n = “Ryan”[3] 1. In the above example, we cr…
  5. 5
    Great work! Now that we know how to store strings, let’s see how we can change them using string methods. String methods let you perform specific tasks for strings. We’ll focus on four st…
  6. 6
    Well done! You can use the lower() method to get rid of all the capitalization in your strings. You call lower() like so: “Ryan”.lower() which will return “ryan”.
  7. 7
    Now your string is 100% lower case! A similar method exists to make a string completely upper case.
  8. 8
    Now let’s look at str(), which is a little less straightforward. The str() method turns non-strings into strings! For example: str(2) would turn 2 into “2”.
  9. 9
    Let’s take a closer look at why you use len(string) and str(object), but dot notation (such as “String”.upper()) for the rest. lion = “roar” len(lion) lion.upper() Methods that use dot notation …
  10. 10
    The area where we’ve been writing our code is called the editor. The console (the window to the right of the editor) is where the results of your code is shown. print simply displays your…
  11. 11
    Great! Now that we’ve printed strings, let’s print variables
  12. 12
    You know about strings, and you know about arithmetic operators. Now let’s combine the two! print “Life “ + “of “ + “Brian” This will print out the phrase Life of Brian. The + operator between …
  13. 13
    Sometimes you need to combine a string with something that isn’t a string. In order to do that, you have to convert the non-string into a string. print “I have “ + str(2) + “ coconuts!” This wil…
  14. 14
    When you want to print a variable with a string, there is a better method than concatenating strings together. name = “Mike” print “Hello %s” % (name) The % operator after the string is used to …
  15. 15
    Remember, we used the % operator to replace the %s placeholders with the variables in parentheses. name = “Mike” print “Hello %s” % (name) You need the same number of %s terms in a string as the…
  16. 16
    Great job! You’ve learned a lot in this unit, including: Three ways to create strings ‘Alpha’ “Bravo” str(3) String methods len(“Charlie”) “Delta”.upper() “Echo”.lower() Printing a string pr…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo