coding180 icon
Coding180
Python String Datatypes

Python String Datatypes

Home > Tutorials > Python for beginners > Python Datatypes


In Python, we use the term "string" to refer to a sequence of characters. For example, a word, a sentence, or even a whole paragraph can be considered a string.

Here are some examples of strings:

"Hello, World!" 
"The quick brown fox jumps over the lazy dog." 
"Python is a programming language."

In Python, we can create a string by enclosing our sequence of characters in quotation marks. For example, to create the string "Hello, World!", we would write the following code:

my_string = "Hello, World!"

Once we have created a string, we can access its individual characters by using their index, which is their position within the string. For example, to access the first character of the string "Hello, World!", we would use the following code:

my_string = "Hello, World!" 
first_char = my_string[0]

Here, the variable first_char would contain the value "H", which is the first character of the string.

We can also use a for loop to iterate over the characters in a string. For example, the following code would print out each character of the string "Hello, World!" on a separate line:

my_string = "Hello, World!" for char in my_string: print(char)

The output of this code would be:

Hello, World!

String Datatypes Quiz

  1. What is a "string" in Python?
    1. A: A sequence of characters
    2. B: A sequence of numbers
    3. C: A sequence of words
    4. D: A sequence of commands
  2. How do we create a string in Python?
    1. A: By enclosing the sequence of characters in square brackets []
    2. B: By enclosing the sequence of characters in curly braces {}
    3. C: By enclosing the sequence of characters in parentheses ()
    4. D: By enclosing the sequence of characters in quotation marks ""
  3. How do we access individual characters in a string in Python?
    1. A: By using their index, which is their position within the string
    2. B: By using their index, which is their order within the string
    3. C: By using their name
    4. D: By using their value
  4. How can we iterate over the characters in a string in Python?
    1. A: By using a for loop
    2. B: By using a while loop
    3. C: By using a do-while loop
    4. D: By using a foreach loop
  5. What will the following code output?
    my_string = "Hello, World!" first_char = my_string[0] print(first_char)
    1. A: "H"
    2. B: "Hello, World!"
    3. C: 0
    4. D: "hello"

user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel