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

Practical 3 string

Uploaded by

SK Roy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Practical 3 string

Uploaded by

SK Roy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Practical 3

A String is a data structure in Python that represents a sequence of characters. It


is an immutable data type, meaning that once you have created a string, you
cannot change it. Strings are used widely in many different applications, such as
storing and manipulating text data, representing names, addresses, and other
types of data that can be represented as text.
What is a String in Python?
Python does not have a character data type, a single character is simply a string
with a length of 1.
Example:
"Geeksforgeeks" or 'Geeksforgeeks' or "a"

print("A Computer Science portal for geeks")

print('A')

Output:
A Computer Science portal for geeks
A
Creating a String in Python
Strings in Python can be created using single quotes or double quotes or even
triple quotes. Let us see how we can define a string in Python.
Example:
In this example, we will demonstrate different ways to create a Python String. We
will create a string using single quotes (‘ ‘), double quotes (” “), and triple double
quotes (“”” “””). The triple quotes can be used to declare multiline strings in
Python.
 Python3

# Python Program for

# Creation of String

1|Page
# Creating a String

# with single Quotes

String1 ='Welcome to the Geeks World'

print("String with the use of Single Quotes: ")

print(String1)

# Creating a String

# with double Quotes

String1 ="I'm a Geek"

print("\nString with the use of Double Quotes: ")

print(String1)

# Creating a String

# with triple Quotes

String1 ='''I'm a Geek and I live in a world of "Geeks"'''

print("\nString with the use of Triple Quotes: ")

print(String1)

# Creating String with triple

# Quotes allows multiple lines

String1 ='''Geeks

2|Page
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
String with the use of Triple Quotes:
I'm a Geek and I live in a world of "Geeks"
Creating a multiline String:
Geeks
For
Life
Accessing characters in Python String
In Python, individual characters of a String can be accessed by using the method
of Indexing. 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.
While accessing an index out of the range will cause an IndexError. Only
Integers are allowed to be passed as an index, float or other types that will cause
a TypeError.

Python String indexing

Example:
In this example, we will define a string in Python and access its characters using
positive and negative indexing. The 0th element will be the first character of the
string whereas the -1th element is the last character of the string.

3|Page
 Python3

# Python Program to Access

# characters of String

String1 ="GeeksForGeeks"

print("Initial String: ")

print(String1)

# Printing First character

print("\nFirst character of String is: ")

print(String1[0])

# Printing Last character

print("\nLast character of String is: ")

print(String1[-1])

Output:
Initial String:
GeeksForGeeks
First character of String is:
G
Last cha racter of String is:
s

4|Page
String Slicing
In Python, the String Slicing method is used to access a range of characters in the
String. Slicing in a String is done by using a Slicing operator, i.e., a colon (:). One
thing to keep in mind while using this method is that the string returned after
slicing includes the character at the start index but not the character at the last
index.
Example:
In this example, we will use the string-slicing method to extract a substring of the
original string. The [3:12] indicates that the string slicing will start from the 3rd
index of the string to the 12th index, (12th character not including). We can also
use negative indexing in string slicing.
 Python3

# Python Program to

# demonstrate String slicing

# Creating a String

String1 ="GeeksForGeeks"

print("Initial String: ")

print(String1)

# Printing 3rd to 12th character

print("\nSlicing characters from 3-12: ")

print(String1[3:12])

5|Page
# Printing characters between

# 3rd and 2nd last character

print("\nSlicing characters between "+

"3rd and 2nd last character: ")

print(String1[3:-2])

Output:
Initial String:
GeeksForGeeks
Slicing characters from 3-12:
ksForGeek
Slicing characters between 3rd and 2nd last character:
ksForGee

Reversing a Python String


By accessing characters from a string, we can also reverse strings in Python. We
can Reverse a string by using String slicing method.
Example:
In this example, we will reverse a string by accessing the index. We did not
specify the first two parts of the slice indicating that we are considering the
whole string, from the start index to the last index.
 Python3

#Program to reverse a string

gfg ="geeksforgeeks"

print(gfg[::-1])

Output:
skeegrofskeeg
Example:
We can also reverse a string by using built-in join and reversed functions, and
passing the string as the parameter to the reversed() function.
 Python3

6|Page
# Program to reverse a string

gfg ="geeksforgeeks"

# Reverse the string using reversed and join function

gfg ="".join(reversed(gfg))

print(gfg)

Output:
Skeegrofskeeg

7|Page
#3. Python code to demonstrate string length

# using len

str = "geeks"

print(len(str))

8|Page

You might also like