KEMBAR78
String Datatype | PDF | String (Computer Science) | Data Type
0% found this document useful (0 votes)
14 views22 pages

String Datatype

python datatype material

Uploaded by

perarasum19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views22 pages

String Datatype

python datatype material

Uploaded by

perarasum19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Programming in Python

(Data Type: String)

Dr. Faisal Anwer


Department of Computer Science
Aligarh Muslim University 1
Copyright © Dept of Computer Science, AMU, Aligarh. Permission required for reproduction or display.
Recap
• Positional arguments

• Keyword arguments

• Default arguments

• Variable-length arguments
Contents of Week
• String Datatype

• List

• Tuple

• Set

• Dictionary
Contents
• Creating String variable

• Accessing String

• String Slicing

• Operations & Functions of String


Data Types
Data Type
 It consists of set of values and set of operations that can be
performed on the values.

 Every variable is an object in Python programming, variables


of data types are actually the instances (objects)

Python-Data Types

Numeric Boolean Sequence Collection

Integer Float String List

Complex Tuple Set Dictionary


Data Types-Sequence
• Sequences are object types in Python that allows the user to store
data one after each other.
• Examples:
• Strings
• Lists
• Tuples Python-Sequences

Common Common
Operations Functions

Slicing Concatenate len() min() & max()

Membership Repeat Index() Count()


Strings
• A string is a sequence of characters .
• The strings start and end with same quotes (single/double/triple).
• The strings can be created by simply enclosing characters in
quotes or using str() function.

• Example:
• ―‗Python‘‖
• ‗5,000‘
• string1 = ―Python Programming‖
• string2 = str(‗IGNTU‘)

Note: Python does not support a character type. CHARACTER is treated


as string of length one.
Strings
Accessing strings
 The values in a string can be accessed using index.

 An index is a position in the sequence. The list index can be either


positive or negative
 Positive index starts from left of list and it ranges from 0 to (length
of list) minus 1.
 Negative index starts from right side of list and it ranges from -1 to
minus length of list (i.e. –length ).

Example
 String1 = ―Delhi‖
 String1[1], String1[8//2 ], String1[10], String1[-3]
Strings are Immutable
 Strings are immutable i.e. the character sequence in strings
can not be changed.

 However, the existing string variable can be updated by


(re)assigning it to another string.

Example
 String1 = ‗What‘
 String1[0] = ‗T‘ —— Invalid
 String1 = ‗That‘
Strings: Operations & Functions

Python-Sequences

Common Operations Common Functions

Slicing Concatenate len() min() & max()

Membership Repeat Index() Count()


Strings: Slicing
 A slice is an string consisting of elements of given strings
 Consecutive part or a substring of string
 Elements from different positions

 Slicing requires-three index values low, high and step_value i.e.,


String[low: high: step_value].

 Default value is 0 for low, 1 for step_value and (total length -1) for
high.

 String[low: high] -- Substring starting from low index and ending at
the index one position before the high

 Example:
 String1 = ‗Kolkata‘ , String1[1:3] – ‗ol‘, String1[0:5:2] – ‗Kla‘
Strings: Operations
a = ‗Python‘; b = ‗Programming'

Concatenation (+)
 The concatenation operator concatenates two strings.

Repetition (*)
 The * operator concatenates a string zero or more times.

Membership operators (in/not in)


 The in operator returns true if a character exists in the given string.

Example: ‗P‘ in ‗Python‘ returns True.


‗P‘ not in ‗Python‘ returns False.
Strings: Sequence Functions
a = ‗Python‘; b = ‗Programming‘

len() – len(a)
 It returns the length of the string.

min() & max() --- min(a) & max(a)


 With a single iterable argument, return its smallest item.
 With two or more arguments, return the smallest argument.

Example: min(a) returns ‗P‘, min(a, b) returns ‗Programming‘


String Traversal using
Loop
#Using for loop:
A = Kolkata'
for char in A:
print(char)

for i in range(len(A)):
print(A[i])

#Using While Loop


A = “Kolkata”
i=0
while i < len(A):
print(A[i])
i=i+1
Strings: Methods
 The methods behave like a function but have a slightly different
syntax.

 A method is always called with a given data value(known as


object), which is placed before the method name in the call.

 The syntax of a method call is:


Object_name.method_name(arg1, arg2, ….)
Strings: Methods
casefold(): Converts string into lower case.

swapcase(): Inverts case for all letters in the string.

lower(): Converts all uppercase letters in a string to lowercase.

islower(): Returns true if the string has all lowercase characters and
false otherwise.

upper(): Converts lowercase letters in a string to uppercase.

isupper(): Returns true if the string has all uppercase characters and
false otherwise.
Strings: Methods
isalpha(): Returns true if the string has all alphabetic characters
and false otherwise.

isdigit(): Returns true if the string contains only digits and false
otherwise.

isalnum(): Returns true if the string has all alpha-numeric


characters and false otherwise.

join(): The string whose method is called is inserted in between


each given string. The result is returned as a new string.
Example
• print(‗AMAR'.casefold())
• print(‗Amar'.swapcase())
• print('INDIA'.lower())
• print('india'.islower())
• print('INDIA'.upper())
• print('India'.isupper())
• print('India2022'.isalpha())
• print('India2022'.isdigit())
• print('India'.isalnum())
• print(' '.join(['India','2022']))
Strings: Methods
a = ‗Python Programming‘, b = ‗is high level‘, c = ‗programming
language‘

 split(): Splits string according to delimiter str (space if not


provided) and returns list of substrings;

Example-
D=“ “.join([a,b,c])
d = ‘Python Programming is high level programming
language’
a.split()  ['Python', 'Programming', 'is', 'high', 'level',
'programming', 'language']
Exercise
1. Write Python Program to Check Whether a String is Palindrome
or Not
2. Write a Python program that first accept a string and If the string
length is less than 4, return empty string. Then create a string
made of the first 2 and the last 2 chars from the accepted string.
Sample String : ‗New Delhi'
Expected Result : ‗Nehi'
Sample String : ‗New'
Expected Result : Empt String
Sample String : ' w'
Expected Result : Empty String
Exercise
3. Write a Python program to delete ‗e‘ from the end of a given
string (length should be at least 3) and add ‗en' at the end. If the
string length of the given string is less than 3, leave it
unchanged.
Sample String : ‗Give'
Expected Result : ‗Given'
Sample String : ‗take'
Expected Result : ‗taken‘
Sample String : ‗ta‘
Expected Result : ‗ta‘
Summary
• Creating String variable

• Accessing String

• String Slicing

• Operations & Functions of String

You might also like