STRING
STRING
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)
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 = '''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