Practical 3 string
Practical 3 string
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
# Creation of String
1|Page
# Creating a String
print(String1)
# Creating a String
print(String1)
# Creating a String
print(String1)
String1 ='''Geeks
2|Page
For
Life'''
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.
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
# characters of String
String1 ="GeeksForGeeks"
print(String1)
print(String1[0])
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
# Creating a String
String1 ="GeeksForGeeks"
print(String1)
print(String1[3:12])
5|Page
# Printing characters between
print(String1[3:-2])
Output:
Initial String:
GeeksForGeeks
Slicing characters from 3-12:
ksForGeek
Slicing characters between 3rd and 2nd last character:
ksForGee
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"
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