0% found this document useful (0 votes)
9 views

STRING

STRTH GT

Uploaded by

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

STRING

STRTH GT

Uploaded by

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

String literals in python are surrounded by either single quotation marks, or

double quotation marks.

'hello' is the same as "hello".

You can display a string literal with the print() function:

ExampleGet your own Python Server


print("Hello")
print('Hello')

Data types:
Python Data types are the classification or categorization of
data items. It represents the kind of value that tells what
operations can be performed on a particular data. Since
everything is an object in Python programming, Python data
types are classes and variables are instances (objects) of these
classes. The following are the standard or built-in data types in
Python:
 Numeric
 Sequence Type
 Boolean
 Set
 Binary Types( memoryview, bytearray, bytes)

1. Numeric Data Types in Python


The numeric data type in Python represents the data that has a
numeric value. A numeric value can be an integer, a floating
number, or even a complex number. These values are defined as
Python int, Python float, and Python complex classes in Python.
 Integers – This value is represented by int class. It
contains positive or negative whole numbers (without
fractions or decimals). In Python, there is no limit to how
long an integer value can be.
 Float – This value is represented by the float class. It is
a real number with a floating-point representation. It is
specified by a decimal point. Optionally, the character
e or E followed by a positive or negative integer may be
appended to specify scientific notation.
 Complex Numbers – A complex number is represented
by a complex class. It is specified as (real part) + (imaginary
part)j. For example – 2+3j
Note – type() function is used to determine the type of Python
data type. Example: This code demonstrates how to
determine the data type of variables in Python using the
type() function. It prints the data types of
three variables: a (integer), b (float), and c (complex). The output
shows the respective data type Python for each variable.
Python

a = 5
print("Type of a: ", type(a))

b = 5.0
print("\nType of b: ", type(b))

c = 2 + 4j
print("\nType of c: ", type(c))
Output:
Typ of a: <class 'int'
e of b: <class >
Typ <class 'complex'> 'float'
e >
Type of c:
2. Sequence Data Types in Python
The sequence Data Type in Python is the ordered collection of
similar or different Python data types. Sequences allow storing
of multiple values in an organized and efficient fashion. There are
several sequence data types of Python:
 Python String
 Python List
 Python Tuple
String Data Type
Strings in Python are arrays of bytes representing Unicode
characters. A string is a collection of one or more characters
put in a single quote, double-quote, or triple-quote. In Python,
there is no character data type Python, a character is a string of
length one. It is represented by str class. Creating String
Strings in Python can be created using single quotes, double
quotes, or even triple quotes.
Example: This Python code showcases various string creation
methods. It uses single quotes, double quotes, and triple quotes
to create strings with different content and includes a
multiline string. The code also
demonstrates printing the strings and checking their data
types.
Python

String1 = 'Welcome to the Geeks World'


print("String with the use of Single
Quotes: ") print(String1)
String1 = "I'm a Geek"
print("\nString with the use of Double Quotes: ")
print(String1)
print(type(String1))
String1 = '''I'm a Geek and I live in a world of "Geeks"'''
print("\nString with the use of Triple Quotes: ")
print(String1)
print(type(String1))

String1 = '''Geeks
For
Life'''
print("\nCreating a multiline String: ")
print(String1)
Output:
String with the use of Single Quotes:
Welcome to the Geeks World
String with the use of Double Quotes:
I'm a Geek
<class 'str'>
String with the use of Triple Quotes:
I'm a Geek and I live in a world of "Geeks"
<class 'str'>
Creating a multiline String:
Geeks
For
Life
Accessing elements of String
In Python programming, individual characters of a String can be
accessed by using the method of Indexing. Negative Indexing
allows negative address references to access characters from
the back of the String, e.g. - 1 refers to the last character, -2
refers to the second last character, and so on.
Example: This Python code demonstrates how to work with a
string named ‘String1′. It initializes the string with
“GeeksForGeeks” and prints it. It then showcases how to access
the first character (“G”) using an index
of 0 and the last character (“s”) using a negative index of -1.
Python

String1 =
"GeeksForGeeks"
print("Initial String:
") print(String1)
print("\nFirst character of String
is: ") print(String1[0])
print("\nLast character of String
is: ") print(String1[-1])
Output:
Initial String:
GeeksForGeeks
First character of String
is: G
Last character of String
is: s
Note – To know more about strings, refer to Python String.
List Data Type
Lists are just like arrays, declared in other languages which is an
ordered collection of data. It is very flexible as the items in a
list do not need to be of the same type.
Creating a List in Python
Lists in Python can be created by just placing the sequence inside
the square brackets[].
Example: This Python code demonstrates list creation and
manipulation. It starts with an empty list and prints it. It creates
a list containing a single string element and prints it. It creates
a list with multiple string elements and prints selected
elements from the list. It creates a multi-dimensional list (a list
of lists) and prints it. The code showcases various ways to work
with lists, including single and multi-dimensional lists.
Python
List = []
print("Initial blank List:
") print(List)
List = ['GeeksForGeeks']
print("\nList with the use of
String: ") print(List)
List = ["Geeks", "For", "Geeks"]
print("\nList containing multiple
values: ") print(List[0])
print(List[2])
List = [['Geeks', 'For'], ['Geeks']]
print("\nMulti-Dimensional List:
") print(List)
Output:
Initial blan List:
k
[]
List with the use of String:
['GeeksForGeeks']
List containing multiple values:
Geeks
Geeks
Multi-Dimensional List:
[['Geeks', 'For'],
['Geeks']]
Python Access List Items
In order to access the list items refer to the index number. Use
the index operator [ ] to access an item in a list. In Python,
negative sequence indexes represent positions from the end of
the array. Instead of having to compute the offset as in
List[len(List)-3], it is enough to just write List[-3]. Negative
indexing means beginning from the end, -1 refers to the last
Python

List = ["Geeks", "For", "Geeks"]


print("Accessing element from the
list") print(List[0])
print(List[2])
print("Accessing element using negative
indexing") print(List[-1])
print(List[-3])
Output:
Accessing element from the list
Geeks
Geeks
Accessing element usin negative indexing
g
Geeks
Geeks
Note – To know more about Lists, refer to Python List.
Tuple Data Type
Just like a list, a tuple is also an ordered collection of Python
objects. The only difference between a tuple and a list is that
tuples are immutable i.e. tuples cannot be modified after it is
created. It is represented by a tuple class.
Creating a Tuple in Python
In Python Data Types, tuples are created by placing a sequence of
values separated by a ‘comma’ with or without the use of
parentheses for grouping the data sequence. Tuples can contain
any number of elements and of any datatype (like strings,
integers, lists, etc.). Note: Tuples can also be created with a
single element, but it is a bit tricky. Having one element in the
parentheses is not sufficient, there must be a trailing ‘comma’ to
make it a tuple.
Example: This Python code demonstrates different methods of
creating and working with tuples. It starts with an empty tuple
and prints it. It creates a tuple containing string elements and
prints it. It converts a list
into a tuple and prints the result. It creates a tuple from a
string using
PYTHON PROGRAMMING 20CS31P

the tuple() function. It forms a tuple with nested tuples and


displays the result.

III SEM CSE 10

You might also like