Getting Started

Python - IntroductionPython - Hello World ProgramPython - SyntaxPython - Data TypesPython - Variables

Operators

Python - Arithmetic OperatorsPython - Relational OperatorsPython - Logical OperatorsPython - Assignment OperatorsPython - Bitwise OperatorsPython - Membership OperatorsPython - Identity OperatorsPython - Increment and Decrement Operators

Conditions

Python - If Else statement

Loop

Python - While LoopPython - For Loop

Numbers

Python - NumbersPython - Number Conversion

Strings

Python - StringsPython - String OperatorsPython - String FormattingPython - String MethodsPython - String Format Method

List

Python - ListPython - List Methods

Tuple

Python - Tuple

Set

Python - SetPython - Set Methods

Dictionary

Python - DictionaryPython - Dictionary Methods

Functions

Python - FunctionsPython - Functions - Variable length argumentsPython - Lambda Function

Scope of Variables

Python - Scope of Variables

Modules

Python - ModulesPython - Math ModulePython - JSON ModulePython - datetime ModulePython - time Module

I/O

Python - Read input from keyboard

File

Python - File Handling

Exception Handling

Python - Exception Handling

OOP

Python - Classes and ObjectsPython - Class Constructor __init__ methodPython - Class Destructor __del__ methodPython - Built-in Class AttributesPython - InheritancePython - Method OverridingPython - Method Overloading

Package Management

Python - PIP

Python - MySQL

Python - MySQL - Getting StartedPython - MySQL - Insert dataPython - MySQL - Select dataPython - MySQL - Update dataPython - MySQL - Delete data

Python - CSV

Python - Read data from CSV filePython - Write data in CSV file

Python - String Operators

Python

python logo

In this tutorial we will learn to operate on strings in Python.

Concat strings

We can concat two string values using the + operator.

In the following example we are concatenating the two strings to get a third string.

# strings
str1 = "Hello"
str2 = "World"

# concat
result = str1 + " " + str2

# output
print(result);

The above program will concat (join) the first string str1 followed by a space and then the second string str2.

So, we will get the following output Hello World.

Replicate strings

We can replicate a given string N times using the * operator.

In the following example we are replicating the string HA 3 times.

# string
str = "HA"

# replicate
result = str * 3

# output
print(result)

The above code will give us a new string HAHAHA.

Checking membership - in

We can use the in operator to check whether a search string is present in a given string. We get True if search string is found, False otherwise.

In the following Python program we are checking if the search string lo is present in the given string Hello World.

# strings
needle = "lo"
haystack = "Hello World"

# check
if needle in haystack:
  print(needle, "is present in the string", haystack)
else:
  print("Not found")

We will get the following output for the above program.

lo is present in the string Hello World

Checking membership - not in

We can use the not in operator to check if a search string is not present in a given string. We get True if search string is not found, False otherwise.

In the following Python program we are checking if the search string HA is present in the given string Hello World.

# strings
needle = "HA"
haystack = "Hello World"

# check
if needle in haystack:
  print(needle, "is present in the string", haystack)
else:
  print("Not found")

We will get Not found as the output of the above code.

Accessing characters of a string

We use the str[i] to get the character at i index in the given string str.

Index starts from 0 so, first character is at index 0, second is at 1 and so on.

In the following example we are extracting the second character (index 1) from the string "Jane Doe".

# string
str = "Jane Doe"

# character
ch = str[1]

# output
print(ch)      # a

Substring

We use str[start:end] to get a substring from a given string.

We start from the start index and extract substring till the end index without including it.

In the following example we are extracting the substring lo from the string Hello World.

# string
str = "Hello World"

# substring
substr = str[3:5]

# output
print(substr)     # lo

Skipping characters

We can skip characters of a string using str[start:end:step].

Where, start is the starting index. end represents the last index (not included) till the string is extracted and step is the number of steps to take.

In the following example we are skipping 1 character for the given string "Hello World".

The string "Hello World" has 11 characters and we want to skip odd indexed characters so, step will be 2.

Since we are considering the entire string so, we can ignore end. We will use str[start::step].

So, we will consider the following characters: 0->2->4->6->8->10.


string:    Hello World
skipping:   x x x x x    # characters marked with x
final str: HloWrd
# string
str = "Hello World"

# skip
new_str = str[0::2]

Reverse string

The easiest way to reverse a string in Python is by writing str[::-1].

In the following Python program we are reversing the string "Hello World".

# string
str = "Hello World"

# reverse
result = str[::-1]

# output
print(result)

The above code will give us the following output dlroW olleH.

Escape Sequence

Escape sequence represents non-printable characters. They start with backslash.

Following are some of the escape sequence.

DescriptionEscape sequence notation
Alert or Bell\a
Backspace\b
Escape\e
Form feed\f
New line\n
Carriage return\r
Space\s
Tab\t