String in Python

A String is a sequence of characters. In Python string literals are enclosed in either single quotes or double quotes. 'Hello World' and "Hello World" are same.

Creating a String Variable

To create a string variable in Python, simply a variable name is declared followed by an equal sign = and then the string value enclosed in either single quotes or double quotes.

Example:

x = 'Hello World'
y = "Hello World"
print(x)
print(y)

Output:

Hello World
Hello World

Assigning a Multiline String

To assign a multiline string to a variable in Python, the string literals are enclosed in triple single or double quotes.

Example:

a = """To assign a multiline string
to a variable in Python, the
string literals are enclosed
in triple quotes."""

b = '''To assign a multiline string
to a variable in Python, the
string literals are enclosed
in triple quotes.'''

print(a)
print(b)

Output:

To assign a multiline string
to a variable in Python, the
string literals are enclosed

To assign a multiline string
to a variable in Python, the
string literals are enclosed

Find String Length

Use the len() function to find the length of a string.

Example:

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

Output:

11

Accessing Single Character of a String

There is no Character data type in Python. A single character having length of 1 is also a string in Python.

In python, a string is an array of Unicode characters. A single character of a string can be accessed using square brackets. The first character always starts from 0 position.

Example:

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

Output:

e

String Slicing

Slicing in Python means getting a range of characters from a string. To return a range of characters, specify the start index and the end index separated by a colon with the square brackets.

Syntax:

string[start index : end index]

Example:

a = "Hello World"
print(a[6:11])

Output:

World

You can also get the range of characters beginning from the end to the start of the string by using negative index.

Syntax:

string[-start index: -end index]

Example:

a = "4444 2222 5555 9999"
print(a[-19:-4])

Output:

4444 2222 5555

String in

The in keyword is used to check if a certain character is present in a string.

Example:

A = "This is a great world"
b = "great" in A
print(b)

Output:

True

String not in

The not in keyword is used to check if a certain character is not present in a string.

Example:

A = "This is a great world"
b = "great" not in A
print(b)

Output:

False

String Format

The format() method is used to format a specified value in a string. It takes arguments, formats them and inserts them in places where curly braces are in a string.

Example:

order_string = "The cost price of this pen is {} and the selling price is {}"
formatted_string = order_string.format(10,15)
print(formatted_string)

Output:

The cost price of this pen is 10 and the selling price is 15

String Concatenation

String concatenation is the joining of two or more strings together. To concatenate two or more string use the + operator.

Example:

string_1 = "Hello"
string_2 = "How are you?"
string_3 = "Have a great day!"
string_4 = "Where do you live?"

message = string_1 + " " + string_2 + " " + string_3 + " " + string_4;
print(message)

Output:

Hello How are you? Have a great day! Where do you live?

Convert String to Lowercase

The lower() function is used to convert string to lowercase.

Example:

a = "HELLO WORLD"
result = a.lower()
print(result)

Output:

hello world

Convert String to Uppercase

The upper() function is used to convert string to uppercase.

Example:

a = "hello world"
result = a.upper()
print(result)

Output:

HELLO WORLD

String Methods

String has a set of useful built-in methods. Some of the methods are:

b = a.count("is")
String MethodDescriptionExample
capitalize()
Converts the first letter of a string to capital.
x = "hello world"
y = x.capitalize()
count()
Count and return the number of times a specified character occurs in a string.a = "This is a great"
b = a.count("is")
decode()
Decodes a value to string.
x = "hello world"
y = x.decode("rld")
encode()
Encodes a string.
x = "hello world"
y = x.encode("rld")
endswith()
Returns true if a string ends with a specified value.
x = "hello world"
y = x.endswith("rld")
find()
Searches the string for a specified character and returns its position.
a = "This is a great"
b = a.find("is")
lower()
Returns the string in lowercase.
x = "Hello World"
y = x.lower()
replace()
Replaces old string with new string.
x = "Hello World"
y = x.replace("H", "h")
rstrip()
Removes any trailing whitespace and returns a new string.
x = "Hello World "
y = x.strip()
strip()
Removes any leading and trailing whitespace and returns a new string.
x = " Hello World "
y = x.strip()
split()
Splits a string if the specified splitting character is found and returns an array.
x = "Hello, World"
y = x.split(",")
swapcase()
Converts uppercase to lowercase and lowercase to uppercase.
x = "Hello World"
y = x.swapcase()
startswith()
Returns true if a string starts with a specified value.
x = "hello world"
y = x.startswith("he")
title()
Changes the first character of every word to uppercase.
x = "hello world"
y = x.title()
upper()
Returns the string in uppercase.
x = "Hello World"
y = x.upper()