4. String in Python
String: A contiguous set of characters which is represented in quotation marks.
str1="Welcome to Second research practice python class"
print(str1)
Output:
Welcome to Second research practice python class
print("First Character of String ", str1[0])
Output: First Character of String W
print(" String from 11th to 17th ", str1[11:17])
Output: String from 11th to 17th Second
print(" String string from 11th ", str1[11:])
output: String string from 11th Second research practice python class
#Printing String Two Times
print(str1*2)
Output: Welcome to Second research practice python classWelcome to Second research practice python class
prints concatenated string
print(str1+ " at BITS Pilani Dubai Campus")
Output: Welcome to Second research practice python class at BITS Pilani Dubai Campus
Escape Characters in Python:
\a : bell or alert
\b: backspace
\f: formfeed
\n : newline
\r: carriage return
\s: space
\t: tab
\v: vertical tab
String Frmatting Operator
%c : character
%s : st ring
%i or %d : singed decimal integer
%u: unsigned decimal integer
%o : octal integer
%x: hexademical
%e: explonetinal number
%f: floating point real number
print("%o is the octal octal equivalent of %d" %(8,8))
output: 10 is the octal octal equivalent of 8
String Functions
len(string)
lower()
upper()
swapcase()
capitalize()
title()
lstrip()
rstrip()
strip()
max(string)
min(string)
replace(old, new)
center(width, fillchar)
ljust(widhth,[,fillchart])
rjust(width,[fillchar])
zfill(width)
count(str, beg=0, end=len(string)
find(str, beg, end)
rfind()
index(str, beg=0, end=len(str)
startwith()
endwith()
isdecimal()
isalpha()