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)
WelcomeWelcomeWelcomeWelcomeWelcome

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)
100
>>> print(type(str1))
<type 'str'>




String Length

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

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

String Iteration

Python has in-built function to iterate over string.

Like -
>>> x = "Welcome"
>>> for c in x:
	print(c)

	
W
e
l
c
o
m
e
>>> 

String Search

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

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

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
True
>>> 'a' in x
False
>>>



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 -

ljust()

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

exp.ljust(n)

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

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

rjust()

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

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

center()

The center() method is used to center a string expression to the given centered length n. The general syntax is -

exp.center(n)
Example of center()
>>> x = "HELLO"
>>> x.center(10)
'  HELLO   '

count()

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

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

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()
'elephant'
>>> x.upper()
'ELEPHANT'

find() method

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

y.find(x)

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

>>> x = "Elephant"
>>> x.find("p")
3
>>> x.find("a")
5
>>> x.find("o")
-1




Read more articles


General Knowledge



Learn Popular Language