0% found this document useful (0 votes)
2 views4 pages

String

The document explains how to create and manipulate strings in Python, including using single, double, and triple quotes. It covers indexing, both positive and negative, to access individual characters, as well as slicing to extract substrings from a string. Examples illustrate these concepts, demonstrating how to print specific characters and reverse strings.

Uploaded by

rj0110865
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)
2 views4 pages

String

The document explains how to create and manipulate strings in Python, including using single, double, and triple quotes. It covers indexing, both positive and negative, to access individual characters, as well as slicing to extract substrings from a string. Examples illustrate these concepts, demonstrating how to print specific characters and reverse strings.

Uploaded by

rj0110865
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/ 4

String

Python string is the collection of the characters surrounded by single quotes, double quotes,
or triple quotes. The computer does not understand the characters; internally, it stores
manipulated character as the combination of the 0's and 1's.
Creating String in Python
We can create a string by enclosing the characters in single-quotes or double- quotes.
Python also provides triple-quotes to represent the string, but it is generally used for
multiline string.
Example
1. #Using single quotes
2. str1 = 'Hello Python'
3. print(str1)
4. #Using double quotes
5. str2 = "Hello Python"
6. print(str2)
7.
8. #Using triple quotes
9. str3 = '''''Triple quotes are generally used for
10. represent the multiline or
11. docstring'''
12. print(str3)

13. Strings indexing and splitting


Like other languages, the indexing of the Python strings starts from 0.
In Python, indexing and slicing are techniques used to access and
manipulate elements in sequences such as strings, lists, and tuples.
Indexing
Indexing is used to access a single element of a sequence. The index
of an element is its position within the sequence. Python uses zero-
based indexing, which means the first element is at index 0.
My_ list = [10, 20, 30, 40, 50]

print(my _list[0]) # Output: 10


print(my _list[3]) # Output: 40
Negative indexing can be used to access elements from the end of
the sequence:
print(my_list[-1]) # Output: 50
print(my_list[-2]) # Output: 40

We can access characters in a String in Two ways :


1. Accessing Characters by Positive Index Number
2. Accessing Characters by Negative Index Number

H E L L O S T U D E N T
0 1 2 3 4 5 6 7 8 9 10 11 12
IF read left to right it will start from 0
-13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
If we read right to left from -1
# declaring the string
str = "Hello student"
# accessing the character of str at 0th index
print(str[0])
# accessing the character of str at 6th index
print(str[7])
# accessing the character of str at 10th index
print(str[10])
Output
H
T
E
Accessing Characters by Negative Index Number: In this type of Indexing, we pass the
Negative index(which we want to access) in square brackets. Here the index number starts
from index number -1 (which denotes the last character of a string).

Example 2 (Negative Indexing) :


# declaring the string
str = "Hello student”
# accessing the character of str at last index
print(str[-1])
# accessing the character of str at 5th index from the last
print(str[-5])
# accessing the character of str at 10th index from the last
print(str[-10])
Output
T
U
L
Slicing
Slicing in Python is a feature that enables accessing parts of the sequence. In slicing a string,
we create a substring, which is essentially a string that exists within another string. We use
slicing when we require a part of the string and not the complete string.
Slicing is used to access a subset (slice) of elements from a sequence. Slicing uses a colon (:)
to specify the start, stop, and step.
sequence[start :stop: step]
start: The index of the first element to include (inclusive).
stop: The index of the element to stop at (exclusive).
step: The interval between elements (optional).
Example
my_list = [10, 20, 30, 40, 50]
print(my_list[1:4]) # Output: [20, 30, 40]
print(my_list[:3]) # Output: [10, 20, 30]
print(my_list[::2]) # Output: [10, 30, 50]

Negative Slicing
print(my_list[-4:-1]) # Output: [20, 30, 40]
print(my_list[::-1]) # Output: [50, 40, 30, 20, 10] # Reverses the list
Syntax :
H=”Hello student” H is a varlable
Print(H[4]) output is =o
Print(H[0:7]) this is range in which multiple characters it show hello
If we use
Print(H[o:]) it read all characters
If we use
Print(H[o: :2]) it show 2 increment means it read one skip character

example, the whole string is printed in reverse order.


Python
# Python program to demonstrate
# string slicing

# String slicing
String = 'welcome'

# Prints string in reverse


print(String[::-1])
output is =emoclew

You might also like