Search this site
Embedded Files
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
  • More
    • Home
    • Courses
    • Contact

日本語  ❯

Lesson 1    ❮    Lesson List    ❮    Top Page

1.1 Variables

❯  1.2  Strings

1.3 Numbers

1.4 Lists

1.5 List Methods

1.6 Slice Operator   

⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳  Video   7m 36s
☷  Interactive readings   5m

Defining Strings

A string is a series of characters. Anything inside quotes is considered as a string. 

You can use single (' ') or double quotes (" ") for strings.

Using Variables inside Strings

It can be useful to use a variable inside a string, like to print your name in "Hello my name is {yourname}". 

To do that, put the letter f right before the opening " ".  Then, put braces {  } around the variable you want to use. 
This method is called f-string.

Using a Method on String-object

Let's perform some action to the string. A method is an action that Python can do on a piece of data, written after a period '.'.
Here are some basic string methods:

lower()
changes a string into the lower case.

upper()
changes the string into the upper case.

title()
changes the first character of each word to upper case.

WRITE BETTER CODE
Using \, \t, and \n in String

Instead of using spaces, you can use \t for tab and \n for newline inside a string. 

Using \ at the end of line allows you to continue writing the string into a new line. Try to delete the \ and see what happens.


LEARN FROM ERRORS
SyntaxError in String

One common error when working with string is related to the misuse of quotes.  Quotes symbols is a part of the syntax.

The code gives a SyntaxError: EOL (end of line) while scanning string literal because the two quotes are not the same. 

Change the last quote into a double quote (") to resolve this error.

©2023. All rights reserved.  Samy Baladram,
Graduate Program in Data Science - GSIS - Tohoku University
Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse