Professional Documents
Culture Documents
invalid
ABCDE3456Z
valid
Test Case 2
abcde12345
Invalid
Test Case 3
abcd01234r
Invalid
Strings
Immutable sequence of characters
using int()
String Operations
String Operations
Escape Sequences
Represent Special Characters
>>> s = 'a\nb\tc'
>>> s 'a\nb\tc'
>>> print(s)
a
b c
>>> len(s)
5
Escape Sequences
Length of a String
>>> s = 'a\0b\0c'
>>> s
'a\x00b\x00c'
>>> len(s)
5
>>> print(s)
abc
Length of a String
3 (coded in hexadecimal):
>>> s = '\001\002\x03'
>>> s
'\x01\x02\x03'
>>> len(s)
3
Backslash in Strings
if Python does not recognize the character after a \ as
>>> x
'C:\\py\\code'
>>> len(x)
10
Backslash in Strings
>>> x = "C:\py\code"
>>> x
'C:\\py\\code'
>>> len(x)
10
PAT-1 – PROGRAM
CODE & OUTPUTS:-
Check this
s = "C:\new\text.dat“
>>>s ……………………………..
print(s)
………………………………
s1 = r"C:\new\text.dat“
>>>s1
print(s1) …w\\………………………….
s2 = "C:\\netext.dat“
print(s2)
>>>s2
Opening a File
Practice session with Python Interactive mode….
>>> print(path)
# User-friendly format C:\new\
text.dat
>>> len(path)
15
Basic Operations
>>> 'Ni!' * 4
'Ni!Ni!Ni!Ni!‘
>>> print('-' * 80) # 80 dashes, the easy way
>>> myjob = "hacker"
>>> for c in myjob:
print(c, end=' ')
hacker
Basic Operations
True
Counting
Count the number of ‘a’
Counting
Count the number of ‘a’
Example
word = 'Btechallbranches'
count = 0
if letter == 'a' :
count = count + 1
print count
Answer: 2
Indexing and Slicing
>>> S = 'spam'
extracted
Indexing can be done either from front or from end
('s', 'a')
>>> S='Sliceofspam'
('S', 'a')
('e', 'p')
Slicing
Take a part of a word
slice
>>> S[1:6]
'liceo'
>>>
Slicing
If the second number is beyond the end of the string, it
stops at the end
If we leave off the first or last number of the slice, it is
assumed to be beginning or end of the string respectively
s = ‘spam’
>>>s[:3]
‘spa’
>>>s[1:]
‘pam’
Properties of Slicing
S[:3] - fetches items at offset 0 up to but not including 3
item
top-level copy of S
Extended slicing
X[I:J:K] - means “extract all the items in X, from
items
Extraction is reversed when negative value is
>>> S[1:10:2]
# Skipping items
'bdfhj'
>>> S[::2]
'acegikmo‘
>>> S = 'hello'
>>> S[::−1]
# Reversing items
'olleh'
Extended slicing Examples
>>> S = 'hello'
>>> S[::−1]
# Reversing items
'olleh'
String Conversion Tools
>>> "42" + 1
implicitly
>>> int("42"), str(42) # Convert from/to string
(42, '42')
int("42") + 1
43
'421'
Character code Conversions
115
Character code Conversions
>>> chr(115)
's'
Character code Conversions - Example
>>> S = '5'
>>> S = chr(ord(S) + 1)
>>> S
'6‘
>>> S = chr(ord(S) + 1)
>>> S
'7'
Character code Conversions - Example
>>> ord('5') - ord('0')
5
13
'0b1101'
Concatenation
>>>S1 = ‘Welcome’
>>>S2 = ‘Python’
>>>S3 = S1 + S2
>>>S3
'WelcomePython'
Changing Strings
>>> S = 'spam'
assignment
Changing Strings
>>> S = S + 'SPAM!'
# To change a string, make a new one
>>> S
'spamSPAM!'
>>> S = S[:4] + 'Burger' + S[−1]
>>> S
'spamBurger!'
Replace
>>> S = 'splot'
>>> S
'spamalot'
Formatting Strings
>>> line.rstrip()
>>> line.upper()
True
To check if all letters in a String are in
Uppercase
>>> 'a'.isupper()
False
>>> 'A'.isupper()
True
>>> 'AB'.isupper()
True
>>> 'ABc'.isupper()
False
>>>
To check if all letters in a String are in
Lowercase
isupper() function is used to check if all
letters in a string are in upper case
Examples
>>> 'a'.islower()
True
>>> 'aB'.islower()
False
To check if a sentence is in Title case
?????
b = "Hello, World!"
print(b[-5:-2])
??????
a = "Hello, World!"
print(len(a))
?????
?????
a = "Hello, World!"
print(a.lower())
????
a = "Hello, World!"
print(a.upper())
?????
a = "Hello, World!"
print(a.replace("H", "J"))
?????
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']
?????
?????
txt = "The rain in Spain stays mainly in the plain"
x = "ain" not in txt
print(x)
?????
a = "Hello"
b = "World"
c=a+b
print(c)
????
a = "Hello"
b = "World"
c = a + " " + b
print(c)
?????
age = 36
txt = "My name is John, I am " + age
print(txt)
?????
age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))
?????
quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))
?????
quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
print(myorder.format(quantity, itemno, price))
?????
???
Make the First Letter of the Word Uppercase is Title():-
The translate() method returns a string where some specified characters are
replaced with the character described in a dictionary, or in a mapping table.
The zfill() method adds zeros (0) at the beginning of the string, until it reaches
the specified length.
If the value of the len parameter is less than the length of the string, no filling is
done.
The zfill() method adds zeros (0) at the beginning of
the string, until it reaches the specified length.
Method Description
endswith() Returns true if the string ends with the specified value
index() Searches the string for a specified value and returns the
position of where it was found
isalpha() Returns True if all characters in the string are in the alphabet
islower() Returns True if all characters in the string are lower case
rfind() Searches the string for a specified value and returns the last
position of where it was found
rindex() Searches the string for a specified value and returns the last
position of where it was found
rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a list
split() Splits the string at the specified separator, and returns a list
splitlines() Splits the string at line breaks and returns a list
x=re.search(“Chennai”, txt)
print(x)