Python String

String is a sequence of characters. This must be enclosed within single quote ('...') and double quote ("..."). Strings are immutable, indexable, iterable.

Python String Example

>>> a = "Hello"
>>> b = ' World'
>>> a+b
'Hello World'

The string can have any number of characters in it. If a string contains a single quote within it, such type of string can be written in double quote. Back slash is used with escaping quotes and other special characters.
Like -

>>> c = "I'm Happy to go"
>>> print c
I'm Happy to go

String Operations

We can perform many operations on the strings like -

String Concatenation

We can concatenate two or more than two strings using plus operator.
Like -

>>> str1 = "Hello John,"
>>> str2 = " How are you"
>>> print(str1 + str2)
Hello John, How are you

Empty String

We can declare empty string.

str1 = '';

Repeat String

We can repeat a string multiple number of times, by using multiple operator.

Like -
>>> str1 = "Welcome"
>>> print(str1*5)

Newline Character

The special character escape sequence "\n" is used for newline character or line feed.

>>> x = "Hello John \n How are you?"
>>> x
'Hello John \n How are you?'
>>> print x
Hello John 
 How are you?

As you can see in the above example, the newline character (\n) is displayed as it is in conversational mode but when we print it, the character follows it will appear on the next line.

These are the some other escape sequences -

Character Meaning
\b Backspace
\t Tab
\\ Backslash
\" Double Quote
\' Single Quote

String Type Conversion

By using str() method, we can convert any type to string datatype.

Like -
>>> a1 = 100
>>> print(type(a1))
<type 'int'>
>>> str1 = str(a1)
>>> print(str1)
>>> print(type(str1))
<type 'str'>

String Length

Python has len() method to get the length of a string.

Like -
>>> str3 = "Welcome"
>>> print(len(str3))

String Iteration

Python has in-built function to iterate over string.

Like -
>>> x = "Welcome"
>>> for c in x:


String Search

We can easily check existence of a character in a string.

Like -
>>> str = "Welcome"
>>> "c" in str

If 'c' character exists in string, then it will return 1 means 'Yes' otherwise it will return 0.

Combine String

In python, we can easily combine two strings using join() function.

Like -
>>> str1 = "4 6 7"
>>> str2 = "D F G"
>>> str3 = str1.join(str2)
>>> print(str3)
D4 6 7 4 6 7F4 6 7 4 6 7G

Split String

We can split each word of a sentence by using split() function.

Like -
>>> str1 = "Hello John! How are you?"
>>> splt = str1.split()
>>> print(splt)
['Hello', 'John!', 'How', 'are', 'you?']

Check for substring

The 'in' operator is used to check for a substrings. Like -

>>> x = 'strong'
>>> 't' in x
>>> 'a' in x

String Method

String methods are used to express some operations on strings. Here is the syntax to call a method -

expr.method(arg1, arg2, ..)

These are the some mostly used Python string methods -


The ljust method is used to left justifies a string expression in a field of a given length. The general syntax is -


The exp is the expression string, n is the length of string.

Example of ljust()
>>> "Hello".ljust(6)
'Hello '
>>> "world".ljust(10)
'world     '


The rjust method is used to right justifies a string expression in a field of a given length. The general syntax is -

Example of rjust()
>>> "Hello".rjust(6)
' Hello'
>>> "World".rjust(10)
'     World'


The center() method is used to center a string expression to the given centered length n. The general syntax is -
Example of center()
>>> x = "HELLO"
'  HELLO   '


The count() method searches a given string x to see how many times string y occures in it.

Example of count()
>>> x = "elephant"
>>> x.count("e")
>>> x.count("a")
>>> x.count("ele")

lower() and upper()

The lower() method is used to convert all strings to lowercase and upper() method is used to convert all strings to uppercase.

>>> x = "Elephant"
>>> x.lower()
>>> x.upper()

find() method

The find() method search for a string x within a string y.


The above method returns position where the first match begins otherwise it returns -1.

>>> x = "Elephant"
>>> x.find("p")
>>> x.find("a")
>>> x.find("o")