PYTHON
CONTENTS
INTRODUCTION
REPLACE() & JOIN()
SPLIT() & REVERSE()
UPPERCASE & LOWERCASE
STRIP() & COUNT()
FORMAT() &LEN()
FIND()
INTRODUCTION
• In Python, Strings are arrays of bytes representing Unicode characters. However, Python does not have a
character data type, a single character is simply a string with a length of 1.
• Example : var1 = “Python!"
var2 = "Software Testing“
print ("var1[0]:",var1[0])
print ("var2[1:5]:",var2[1:5])
Output
var1[0]: P var2[1:5]: oftw
Operator Description Example
[] Slice- it gives the letter from the a[1] will give “y" from the word Python as such ( 0=P, x="Python" print (x[1])
given index 1=y, 2=t, 3=h, 4=o and 5=n)
[:] Range slice-it gives the characters x [1:3] it will give “yt" from the word Python. x="Python" print (x[1:3])
from the given range Remember it will not consider 0 which is G, it will
consider word after that is yt.
in Membership-returns true if a y is present in word Python and hence it will give 1 x="Python" print (“y" in x)
letter exist in the given string (True)
not in Membership-returns true if a l not present in word Python and hence it will give 1 x="Python" print ("l" not in x)
letter exist is not in the given
string
r/R Raw string suppresses actual Print r'\n' prints \n and print R'/n' prints \n
meaning of escape characters.
% - Used for %r - It insert the canonical string The output of this code will be "Python 99". name = 'Python' number = 99
string format representation of the object (i.e., print ('%s %d' %
repr(o)) %s- It insert the (name,number))
presentation string
representation of the object (i.e.,
str(o)) %d- it will format a
number for display
+ It concatenates 2 strings It concatenate strings and gives the result x="Python" y="99" print (x+y)
* Repeat It prints the character twice. x="Python" y="99"
REPLACE & JOIN
• REPLACE : The method replace() returns a copy of the string in which the values of old string have been
replaced with the new value.
• EXAMPLE : oldstring = 'I like python'
newstring = oldstring.replace('like', 'love')
print(newstring)
Output
I love python
• JOIN : The join function is a more flexible way for concatenating string. With join function, you can add any
character into the string.
• EXAMPLE : Add a colon (:) after every character in the string "Python" you can use the following code.
print(":".join("Python"))
Output
P:y:t:h:o:n
SPLIT & REVERSE
• SPLIT : Split strings is another function that can be applied in Python.
• The split() method is used to split a string into list.
• EXAMPLE :
word=“hello python "
print(word.split(' '))
Output
[‘hello', ‘python']
• REVERSE : Python reverse() method reverses elements of the list. If the list is empty, it simply returns
an empty list. After reversing the last index value of the list will be present at 0 index.
• EXAPMLE :
string=“apple”
string.reverse()
print(string)
Output
[‘e’, ’l’, ’p’, ’p’, ’a’]
UPPERCASE & LOWERCASE
• UPPERCASE : The upper( ) is a built-in method used for string handling. It returns a string with
uppercase.
• EXAMPLE :
string=“hello python"
print(string.upper())
Output
HELLO PYTHON
• LOWERCASE : The lower( ) is a built-in method used for string handling. It returns a string with
lowercase.
• EXAMPLE :
string=“HELLO PYTHON"
print(string.lower())
Output
Hello python
STRIP & COUNT
• STRIP : strip() is a built-in method used to remove all the leading and trailing spaces and characters from
a string.
• EXAPMLE : string=“####hello python####"
print(string.strip(‘#’))
Output
hello python
• COUNT : count() is a built-in method that returns an integer that denotes the number of times a substring
occurs in a string.
• EXAMPLE :
string=“hello python"
print(string.count(“o”))
Output
2
FORMAT & LEN
• FORMAT : format() is method used to allow multiple substations and value formatting. This method lets
us concatenate elements within a string through positional formatting.
• EXAMPLE :
string=“hello python {}"
print(string.format(“language”))
Output
Hello python language
• LEN : len() is used to return the total length of string.
• EXAMPLE :
string=“hello python"
print(string.len())
Output
11
FIND
• FIND : The parameters passed to find() method are substring i.e the string you want to search for, start,
and end. The start value is 0 by default, and the end value is the length of the string.
• EXAMPLE :
mystring = “Welcome to python!“
print("The position of python is at:", mystring.find(“python“))
Output:
The position of Tutorials is at: 11
ASSIGNMENT
Positive thinking leads a man to success. One who thinks that he can achieve the things, will put his best to
achieve, will not fetter by the problems in the path of success and one day he will win positively. Self
confidence, determination, perseverance, and hard work are the key factors of success. Dedication, devotion to
the task and positive thinking with determination are the important factors which leads to success.
• In the given context , use replace method to replace the word “fetter” with “restrain”.
• In the given context, use join method to combine the context without any punctuations.
• In the given context, use reverse method to reverse the entire context.
• In the given context, use lowercase method to replace all the uppercase into lowercase.
• In the given context, count the number of occurance of the word “success” and find the length of the
context.
• To get a input text from the user and replace a particular word into “replaced context”.
THANK YOU