Python Strings
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Python Strings

Kishore V

Python Strings

A string in Python is a sequence of characters used to represent text. Strings can be written using either single quotes (' ') or double quotes (" ")—both work exactly the same way.

Displaying a String

You can show text on the screen using the print() function.

Welcome to Python Learning strings is easy

Using Quotes Inside a String

You can include quotation marks inside a string as long as they are different from the quotes used to define the string.

Python is called 'easy to learn' She said, "Practice daily" It's a great day to code

Assigning a String to a Variable

Strings are commonly stored in variables so they can be reused later in a program.

Hello, Developer!

Multi-Line Strings

Python allows you to store text across multiple lines using triple quotes (""" """ or ''' '''). Line breaks in the code are preserved in the output.

Example (Triple Double Quotes)

Python is powerful, easy to read, and widely used.

Example (Triple Single Quotes)

This is a multi-line string. It is useful for long messages or documentation.

Strings as Sequences

In Python, strings behave like sequences (arrays) of Unicode characters. Each character has a position index, starting from 0.

t

This prints the character at index position 2.

Looping Through a String

Since strings are iterable, you can loop through each character using a for loop.

c o d i n g

Finding the Length of a String

Use the len() function to determine how many characters a string contains.

14

Checking for Text Inside a String

You can check whether a word or character exists in a string using the in keyword.

True

Using 'in' with an if Statement

The word 'progress' was found.

Checking if Text Is NOT Present

To verify that a word or character does not exist in a string, use the not in keyword.

True

Using 'not in' with an if Statement

The word 'failure' is not present.

Python String Slicing

String slicing allows you to extract a specific portion of a string by selecting a range of characters. This is done using square brackets [] with a start index and an end index, separated by a colon.

  • The start index is inclusive, and the end index is exclusive.
  • String indexing always begins at 0.

Basic Slice Syntax

gram

Slicing From the Beginning

If you omit the starting index, Python automatically begins slicing from the first character.

Deve

Slicing Until the End

If you leave out the ending index, Python slices the string all the way to the last character.

ista

Using Negative Indexes

Negative indexing allows you to count characters from the end of the string (-1 refers to the last character).

Exam

Modifying Strings in Python

Python provides many built-in methods that allow you to change or manipulate string values easily. These methods do not modify the original string—instead, they return a new modified string.

Converting Text to Uppercase

PYTHON PROGRAMMING

Converting Text to Lowercase

user@example.com

Removing Extra Spaces

Alice Johnson

Replacing Part of a String

I love Python

Splitting a String into a List

['red', 'green', 'blue']

Combining Strings in Python

In Python, string concatenation means joining two or more strings into a single string. This is commonly done using the + operator.

Joining Two Strings

SuriyaR

Adding Space Between Strings

New Delhi

Concatenating Multiple Strings

Python Beginner Online

Python – String Formatting

In Python, strings and numbers cannot be directly joined using the + operator. Attempting to do so will raise a TypeError because Python treats text and numeric values as different data types.

Incorrect Example (Raises Error)

Traceback (most recent call last): File "main.py", line 2, in <module> message = "Current year is " + year TypeError: can only concatenate str (not "int") to str

To safely combine text with numbers, Python provides string formatting techniques such as f-strings.

Basic F-String Example

User Alex has scored 120 points

Using Variables Inside Placeholders

The cost of the Laptop is 45000 rupees

Formatting Floating-Point Numbers

Today's temperature is 36.7°C

Using Expressions Inside F-Strings

Total amount to pay: 995 rupees

Python – Escape Characters

Escape characters are special sequences used in Python strings to represent characters that cannot be typed directly or would otherwise break the string syntax.

Problem Example (Without Escape Characters)

File "main.py", line 1 text = "This is my "Python" program" ^ SyntaxError: invalid syntax

Using Escape Characters Correctly

This is my "Python" program

Commonly Used Escape Characters

Single Quote (\')

It's a sunny day

Backslash (\\)

C:\Users\Admin\Documents

New Line (\n)

Line one Line two Line three

Tab Space (\t)

Name Age City

Carriage Return (\r)

World

Backspace (\b)

Python

Form Feed (\f)

Page1 Page2

Octal Value (\ooo)

A

Hexadecimal Value (\xhh)

A
Our website uses cookies to enhance your experience. Learn More
Accept !