Python String

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.

Assign String to a variable

To assign string literal, a variable is declared followed by an equal sign and then the string value.

Example

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

String is Array of Unicode Characters

In python, 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[2])
  
Output
l

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

Assign Multiline String to a variable

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

String Length

The len() function return the length of a string.

Example

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

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 Concatination

String concatination is the joining of two or more strings together. To concatinate 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?

String Methods

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

String Methods Description Example
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 occcurs 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()