Python Devlopment






String Operations



The str class provides different basic methods to perform various operations on a string. It hepls to calculate the length of a string,to retrieve the individual character from the given string and to compare and concatenate the two different strings.

1. String Comparison

Python String comparison can be performed using equality (==) and comparison (<, >, !=, <=, >=) operators. There are no special methods to compare two strings.

name = 'Python'

print(name == 'Python')
print(name != 'Python)
print(name < 'Python')
print(name > 'Python')
print(name <= 'Python')
print(name >= 'Python')

#Output
True
False
False
False
True
True

2. String Formatting Operators

The strings in Python have a unique built in operation: the % operator(Modulo). This is also called the String Formatting operator and is simillar to the formatting operator used in C language.

# This prints out "Hello, Python!"
name = "Python"
print("Hello, %s!" % name)

#Output
Hello, Python

To use two or more argument specifiers, use a tuple (parentheses):

# This prints out "Vihaan is 8 years old."
name = "Vihaan"
age = 8
print("%s is %d years old." % (name, age))

#Output
Vihaan is 8 years old.

Any object which is not a string can be formatted using the %s operator as well. 

# This prints out: A list: [1, 2, 3]
mylist = [1,2,3]
print("A list: %s" % mylist)

#Output
A list: [1, 2, 3]

%c - Character 

%s - String conversion via str() prior to formatting

%i  - Signed Decimal Integer

%d - Signed Decimal Integer

%u - Unsigned Decimal Integer

%o - Octal Integer

%x - Hexadecimal Integer (Lowercase Letter)

%X - Hexadecimal Integer (Uppercase Letter)

%e - Exponential Notation (With lowercase ‘e’)

%E - Exponential Notation (With uppercase ‘E’ )

%f  - Floating Point real Number 

%g - The shorter of %f and %e

%G - The shorter of %f and %E

 

3. String Formatting built-in Functions

Python includes many built-in functions for strings. few of built-in functions are as follows

capitalize() - Converts first character to Capital Letter

center()      - Pads string with specified character

casefold()   - converts to casefolded strings

count()       - returns occurrences of substring in string

endwith()   - Checks if String Ends with the Specified Suffix

find()         - Returns the index of first occurrence of substring

format()    - formats string into nicer output

index()      - Returns Index of Substring

isalnum()    - Checks Alphanumeric Character

isalpha()     - Checks if All Characters are Alphabets

isdecimal() -Checks Decimal Characters

isdigit()      - Checks Digit Characters

islower()    - Checks if all Alphabets in a String are Lowercase

isupper()    - returns if all characters are uppercase characters

lower()       - returns lowercased string

upper()       - returns uppercased string

replace()    - Replaces Substring Inside

input()       - reads and returns a line of string

float()        - returns floating point number from number, string

int()           - returns integer from a number or string

len()          - Returns Length of an Object

max()        - returns largest element

min()         - returns smallest element

ord()          - returns Unicode code point for Unicode character

reversed()  - returns reversed iterator of a sequence

sorted()     - returns sorted list from a given iterable

sum()        - Add items of an Iterable

 

Lab Assignment:

Write a python program to demonstrate various string functions and operations

s1="hello python"
s2="HELLO PYTHON"

print("Upper Case = ",s1.upper())
print("Lower Case = ",s2.lower())
print(s1.replace("o","u"))
print(s1.capitalize())
print("Number of o are = ",s1.count('o'))
print("Index of substring = ",s1.find('python'))
print("The String is = ",s1.center(30))
print("The String is = ",s1.center(30,"*"))

#Output
Upper Case =  HELLO PYTHON
Lower Case =  hello python
hellu pythun
Hello python
Number of o are =  2
Index of substring =  6
The String is =           hello python         
The String is =  *********hello python*********