0% found this document useful (0 votes)
3 views2 pages

Strings in Python Notes

Strings in Python are sequences of Unicode characters that can be enclosed in various types of quotes. They support operations such as indexing, slicing, concatenation, and have immutable properties, meaning they cannot be changed after creation. Common string methods include case conversion, trimming, searching, and testing for various conditions.

Uploaded by

jothisanjay29
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)
3 views2 pages

Strings in Python Notes

Strings in Python are sequences of Unicode characters that can be enclosed in various types of quotes. They support operations such as indexing, slicing, concatenation, and have immutable properties, meaning they cannot be changed after creation. Common string methods include case conversion, trimming, searching, and testing for various conditions.

Uploaded by

jothisanjay29
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 in Python - Notes

Definition of a String
A string is a sequence of one or more Unicode characters (letters, digits, whitespace, symbols).
Strings can be enclosed in single quotes ('...'), double quotes ("..."), or triple quotes ('''...''' or
"""...""").

Indexing & Slicing


Indexing: Access characters via positions—forward from 0, backward from –1 onwards. Slicing:
str[start:end] to get a substring from start (inclusive) up to end (exclusive). If start > end, slicing
returns an empty string. Out-of-range indices are truncated.

String Operations
Concatenation: Use + to join strings. Example: 'Hello' + 'World' = 'HelloWorld'. Repetition: Use * with
an integer. Example: 'abc' * 2 = 'abcabc'. Membership: Use 'in'/'not in' to check substring presence.
Example: 'a' in 'cat' = True.

Traversing Strings
For loop: for ch in str1: print(ch, end='') While loop: index = 0 while index < len(str1):
print(str1[index], end='') index += 1

String Immutability
Strings in Python are immutable — once created, they cannot be altered. Any change creates a
new string.

Common String Methods


Case conversion: upper(), lower(), capitalize(), title(), swapcase(). Trimming: strip(), lstrip(), rstrip().
Searching/counting: find(), index(), count(). Structure: split(), join(), partition(), replace(). Testing:
isalnum(), isalpha(), isdigit(), islower(), isupper(), isspace(), startswith(), endswith().

Summary Table
Concept Description

String Declaration '...', "...", '''...'''


Indexing str[i], negative indices like -1
Slicing str[start:end]
Concatenation + operator
Repetition * operator
Membership in / not in operators
Traversal for ch in str or while loop
Immutability Strings cannot be changed in-place
Common Methods Case, trim, count, find, split, join, replace, test methods

You might also like