0% found this document useful (0 votes)
16 views7 pages

Understanding Python Strings and Operations

A string is a sequence of characters that can be created using quotes. Characters in a string can be accessed through indexing, with the first character starting at index 0. Strings are immutable, meaning their contents cannot be changed after creation, and operations such as concatenation and repetition can be performed using the + and * operators, respectively.

Uploaded by

dsdggechud
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

Understanding Python Strings and Operations

A string is a sequence of characters that can be created using quotes. Characters in a string can be accessed through indexing, with the first character starting at index 0. Strings are immutable, meaning their contents cannot be changed after creation, and operations such as concatenation and repetition can be performed using the + and * operators, respectively.

Uploaded by

dsdggechud
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

STRINGS-

String is a sequence which is made up of one or more 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.

For ex-

str1 = 'Hello World!'

str2 = "Hello World!"

str3 = """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.

Ex-

str1 = 'Hello World!'

str1[0]

'H'

str1[6] 'W'

str1[11] '!’

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

a. Concatenation-

To concatenate means to join. Python allows us to join two strings using


concatenation operator plus which is denoted by symbol +.

b. Repetition –

Python allows us to repeat the given string using repetition operator which is
denoted by symbol *. str1 = 'Hello'

str1 * 2

'HelloHello'

You might also like