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

Strings

A string in Python is a sequence of one or more UNICODE characters, created by enclosing characters in quotes. Strings are immutable, meaning their contents cannot be changed after creation, and can be accessed using indexing. Various operations such as concatenation, repetition, membership, and slicing can be performed on strings, and Python provides built-in functions for string manipulation.

Uploaded by

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

Strings

A string in Python is a sequence of one or more UNICODE characters, created by enclosing characters in quotes. Strings are immutable, meaning their contents cannot be changed after creation, and can be accessed using indexing. Various operations such as concatenation, repetition, membership, and slicing can be performed on strings, and Python provides built-in functions for string manipulation.

Uploaded by

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

Strin

gs
String is a sequence which is made up of one or more
UNICODE characters. Here the character can be a
letter, digit, whitespace or any other symbol. A string
can be created by enclosing one or more characters in
single, double or triple quote.
Example
>>> str1 = 'Hello World!'
>>> str2 = "Hello World!"
>>> str3 = """Hello World!"""
>>> str4 = '''Hello World!''

Accessing Characters in a String


Each individual character in a string can be accessed
using a technique called indexing. The index specifies
the character to be accessed in the string and is written
in square brackets ([ ]). The index of the first character
(from left) in the string is 0 and the last character is n-1
where n is the length of the string. If we give index
value out of this range then we get an IndexError. The
index must be an integer (positive, zero or negative).
Example
>>> str1 = 'Hello World!'
>>> str1[0]
'H'
>>> str1[6]
'W'
>>> str1[11]
'!'
#gives error as index is out of range
>>> str1[15]
IndexError: string index out of range

String is Immutable
A string is an immutable data type. It means that the
contents of the string cannot be changed after it has
been created. An attempt to do this would lead to an
error.
>>> str1 = "Hello World!"
#if we try to replace character 'e' with 'a' >>> str1[1] =
'a'
TypeError: 'str' object does not support item
assignment
String Operations(concatenation, repetition,
membership and slicing.)
1. Concatenation(‘+’) – To Join
>>> str1 = 'Hello' #First string
>>> str2 = 'World!' #Second string
>>> str1 + str2 #Concatenated strings
'HelloWorld!'
2. Repetition(‘*’) – To repeat a string
>>> str1 = 'Hello' #repeat the value of str1
>>> str1 * 2 'HelloHello'
3. Membership( ‘in’ and ‘not in’)
The 'in' operator takes two strings and returns
True if the first string appears as a substring in the
second string, otherwise it returns False.
>>> str1 = 'Hello World!'
>>> 'W' in str1 True
>>> 'Wor' in str1 True
>>> 'My' in str1 False
4. Slicing(str1[n:m])
In Python, to access some part of a string or
substring, we use a method called slicing. This can
be done by 2024-25 Strings 179 specifying an
index range. Given a string str1, the slice operation
str1[n:m] returns the part of the string str1
starting from index n (inclusive) and ending at m
(exclusive).
>>> str1 = 'Hello World!'
>>> str1[1:5] 'ello'
>>> str1[7:10] 'orl'
>>> str1[3:20] 'lo World!'

Traversing a String
We can access each character of a string or
traverse a string using for loop and while loop. (A)
String Traversal Using for Loop:
>>> str1 = 'Hello World!'
>>> for ch in str1:
print(ch,end = '')
Hello World! #output
while Loop:
>>> str1 = 'Hello World!'
>>> index = 0
>>> while index < len(str1):
print(str1[index],end = '')
index += 1
Hello World! #output

String Methods and Built-in Functions


Python has several built-in functions that allow us
to work with strings. Table 8.2 describes some of
used built-in functions for string manipulation.

You might also like