Data Structures In Python 2

String

Strings are collections of alphabets, words or other characters. In Python, you can create strings by enclosing a sequence of characters within a pair of single or double quotes. For example: ‘cake’, “cookie”, etc.

You can also apply the + operations on two or more strings to concatenate them, just like in the example below:

x = ‘Hello’
y = ‘World’
print(x + ‘ & ‘ + y)
a=x + ‘ & ‘ + y
print(a)

Output:

Hello & World
Hello & World
>>>

Here are some other basic operations that you can perform with strings; For example, you can use * to repeat a string a certain number of times:

# Repeat
x = ‘Hello’
y = ‘World’
print(x*2)
print(y*2)
a=x*3
b=y*3
print(a)
print(b)

Output:

HelloHello
WorldWorld
HelloHelloHello
WorldWorldWorld
>>>

You can also slice strings, which means that you select parts of strings:

x = ‘Hello’
y = ‘World’
# Range Slicing

z1 = x[2:]
z2=x[3:]
print(z1)
print(z2)

# Slicing
z1=x[0] + x[1]
z2 = y[0] + y[1]
print(z1)
print(z2)

Output:

llo
lo
He
Wo
>>>

Note that strings can also be alpha-numeric characters, but that the + operation still is used to concatenate strings.

x = ‘4’
y = ‘2’
z=x + y
print(z)

x = 4
y = 2
z=x + y
print(z)

Output:
42
6
>>>

Python has many built-in methods or helper functions to manipulate strings. Replacing a substring, capitalising certain words in a paragraph, finding the position of a string within another string are some common string manipulations. Check out some of these:

Capitalize strings
a=”hello”
a1=”WORLD”
b=str.capitalize(‘hello’)
print(b)
c=str.upper(a)
print(c)
d=str.lower(a1)
print(d)

Output:

Hello
HELLO
world
>>>

Retrieve the length of a string in characters. Note that the spaces also count towards the final result:

a = “Hello World”
b = “Computer”
a1=len(a)
b1=len(b)
print(“len = “,a1)
print(“len = “,b1)

Output:

len = 11
len = 8
>>>

Check whether a string consists of only digits

a=”Hello”
b=”120″
c=”130a”
print(a.isdigit())
print(b.isdigit())
print(c.isdigit())

Output:
False
True
False
>>>

Replace parts of strings with other strings

a=”hello”
b=”computer”
print(a)
print(b)
c=a.replace(‘ll’,”tt”)
d=a.replace(‘ll’,b)
print(c)
print(d)

Output:

hello
computer
hetto
hecomputero
>>>

Find substrings in other strings; Returns the lowest index or position within the string at which the substring is found:

a = ‘computer’
b = ‘hello’
c=a.find(“t”)
if(c!=-1):
print(“found at location “,c)
else:
print(“String not found”)

d=a.find(“x”)
if(d!=-1):
print(“found at location “,d)
else:
print(“String not found”)

Output:
found at location 5
String not found
>>>

if substring is not present it returns -1. Counting starts from 0.

You can find an exhaustive list of string methods in Python here.

 

Boolean

This built-in data type that can take up the values: True and False, which often makes them interchangeable with the integers 1 and 0. Booleans are useful in conditional and comparison expressions, just like in the following examples:

x = 4
y = 2
print(x == y)
print(x > y)

Output:
False
True
>>>

a=10
b=20
c=a==b
d=a>b
print(c)
print(d)

Output:

False
False
>>>