🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Python Notes
Topic #17

Python Strings


Strings

Strings in python are surrounded by either single quotation marks, or double quotation marks.

'hello' is the same as "hello".

You can display a string literal with the print() function:

Example

print("Hello")
print('Hello')

Quotes Inside Quotes

You can use quotes inside a string, as long as they don't match the quotes surrounding the string:

Example

print("It's alright")
print("He is called 'Johnny'")
print('He is called "Johnny"')

Assign String to a Variable

Assigning a string to a variable is done with the variable name followed by an equal sign and the string:

Example

a = "Hello"
print(a)

Multiline Strings

You can assign a multiline string to a variable by using three quotes:

Example

  a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do
  eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)

Or three single quotes:

Example

  a = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do
  eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''
print(a)

Note: in the result, the line breaks are inserted at the same position as in the code.


Strings are Arrays

Like many other popular programming languages, strings in Python are arrays of unicode characters.

However, Python does not have a character data type, a single character is simply a string with a length of 1.

Square brackets can be used to access elements of the string.

Example

a = "Hello, World!"
print(a[1])

Looping Through a String

Since strings are arrays, we can loop through the characters in a string, with a for loop.

Example

for x in "banana":
  print(x)

Learn more about For Loops in our Python For Loops chapter.


String Length

To get the length of a string, use the len() function.

Example

a = "Hello, World!"
print(len(a))

Check String

To check if a certain phrase or character is present in a string, we can use the keyword in.

Example

  txt = "The best things in life are free!"
print("free" in txt)

Use it in an if statement:

Example

txt = "The best things in life are free!"
if "free" in txt:

print("Yes, 'free' is present.")

Learn more about If statements in our Python If...Else chapter.


Check if NOT

To check if a certain phrase or character is NOT present in a string, we can use the keyword not in.

Example

  txt = "The best things in life are free!"
print("expensive" not in txt)

Use it in an if statement:

Example

txt = "The best things in life are free!"
if "expensive" not in txt:

print("No, 'expensive' is NOT present.")

Want to go beyond the notes?

Join CodingNow 2.0's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Python Strings – FAQs

Quick answers about learning Python Strings in Python.

This free note from CodingNow 2.0 explains Python Strings in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on CodingNow 2.0, including Python Strings, is 100% free with no signup required.
With focused practice, most students grasp Python Strings in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now