Python tutorial : Part 8 : Python String

Python tutorial : Part 8 : Python String
Python String:

To create a String in python, we use a single quote or double quotes. Enclosing characters inside quotes creates a string. Following example will show you to create a python string :

string1 = 'using single quote'
string2 = "using double quotes"

print string1
print string2

The above program will print both of the strings.

Triple Quotes:
With triple quotes, we can write multiline strings in python. It can contain a newline character, tab or any other special characters.

string1 = """This is a \n multiline string with a tab (\t) here."""
string2 ='''This is also a multiline
string'''

print string1
print string2
This program will give the following output: 
This is a
 multiline string with a tab () here.
This is also a multiline
string
Escape Sequence:
An escape sequence is interpreted differently and it starts with a backslash ( \ ).  Following are the list of all escape sequence used in python:
\newline
Backslash and newline ignored
\b
Backspace
\’
Single quote
\”
Double quote
\a
\e
\s
Alert/Bell
escape
space
\\
Backslash
\t
Tab
\v
Vertical Tab
\r
Carriage Return
\f
Formfeed
\n
New Line
\nnn
octal notation
\xnn
hexadecimal notation
Raw Strings:
Normally, backslash ( \ ) is considered as a special character. To ignore it, we can use raw string. A raw string is defined as r’string’.
string1 = "not ignoring\tbackslash"
string2 = r"ignoring\tbackslash"

print string1
print string2
Output will be:
not ignoringbackslash
ignoring\tbackslash
Accessing Values in a String:

Accessing a character is same as accessing elements in lists and tuples. We can use index inside a square bracket for a particular position. For accessing a substring, we can use slicing like string_name[ start: end ]. If start is ‘0’ and end is ‘4′, we will get the substring starting from 0th position up to the 4th position of the string.

Negative indexing is also possible in python strings. If we use ‘-1’ as an index, it will point to the last character, similarly ‘-2’ for the second last and so on.

The following example will clarify your doubt:

string1 = "Hello World!!"

print string1[0] # output : 'H'
print string1[-1] # output : '!'
print string1[0:5] # output : 'Hello'
print string1[5:-1] # output : ' World!’

If we try to use an invalid index, it will throw IndexError.


Updating a String: 

Strings are immutable i.e. we cannot change or delete any character of a string. Instead, we can create a different String.

e.g. change the string “hello world!!” to “hello universe!!”. Since we cannot change any character, instead we can use slicing operation as shown above to create a different string:

string1 = "Hello World!!"
string2 = string1[0:6]+"universe"+string1[-2:]

print string2


Python String Operations:

Concatenation :
The plus ( + ) operator is used to concatenate two strings. 
If we put two strings together, without any + sign, they will concatenate
Two strings on a different line, enclosed in a bracket ( ) will concatenate.

e.g. :

string1 = "Hello" " World!!"
print string1

string2 = ( "Hello"  " World!!")
print string2

string3 ="Hello"

string4 =" World!!"
print string3 + string4

In the example above, all the print statements will give the same output: “Hello World!!”

Repeating Strings:

Using pointer ‘*’ operator, we can repeat a string ’n’ no. of times.

string1 = "Hello "
print string1*3 # prints Hello Hello Hello
MemberShip Check:

Using ‘in’ and ‘not in’, we can check if a character exists in a string. It returns ’True’ if true, ‘False’ otherwise.

string1 = "Hello World!!"

print 'H' in string1 # True
print 'H' not in string1 # False
Formatting String :

Python has string format operator % to format string :

print "Character %c , String %s and a number %d " %('a','Apple',34)
Output :
Character a, String Apple and a number 34

Following are the list of symbols and the meaning of each symbol:

 

d
Signed integer decimal.
i
Signed integer decimal.
o
Unsigned octal.
u
Unsigned decimal.
x
Unsigned hexadecimal (lowercase).
X
Unsigned hexadecimal (uppercase).
e
Floating point exponential format (lowercase).
E
Floating point exponential format (uppercase).
f
Floating point decimal format.
F
Floating point decimal format.
g
Same as “e” if the exponent is greater than -4 or less than precision, “f” otherwise.
G
Same as “E” if the exponent is greater than -4 or less than precision, “F” otherwise.
c
Single character (accepts integer or single character string).
r
String (converts any python object using repr()).
s
String (converts any python object using str()).
%
No argument is converted results in a “%” character in the result.
format() for Formatting String :
We can also use format() to format a String in python. Curly bracket is used as a replacement.
print "Formatting {}, {}, {}".format('a','b','c')
print "Formatting {2}, {1}, {0}".format('a','b','c')
print "Formatting {c}, {b}, {a}".format(a='apple',b='ball',c='cat')
The output will be:
Formatting a, b, c
Formatting c, b, a
Formatting cat, ball, apple

 

No Comments

Post a Comment