You are on page 1of 24

Python - Strings

Strings
 A string is a sequence of characters.
 Character could be a letter, digit, white space or any special symbols.
 Python does not support character data type.
 A single character also works as a string. Python supports writing the
string within a single quote(‘ ‘) and a double quote(“ “).
 Index: Individual character in a string are accessed using subscript([])
operator. Index of first character is 0 and last character is n-1
Example
Indexing in Strings
Positive and negative indexing
String Membership Test
 We can test if a sub string exists within a string or
not, using the keyword in
Concatenation, Appending and
Multiplication of Strings
 Joining of two or more strings into a single one is called concatenation
(using + operator) Append mean to add something at the end.
 To append a string using “+=“ operator
 To repeat a string in n no.of times using ‘*’ operator.
Strings are immutable
 Strings are immutable (can’t change using index)

 You try to change existing str, it create new string


 The id() returns memory address of that object.
‘del’ -> delete Strings
 We can’t delete or remove character from a
string. However, we can delete entire string using
keyword ‘del’.
String Methods
String Methods
String Methods
String Methods
String Methods
String Methods
Strings Formatting operator (%)
%c, %s, %f, %d, %e
Built-in String
methods and functions
 Len(), count()
 Lower(), upper(), title(), capitalize(), swapcase()
 Strip(), lstrip(), rstrip()
 Replace()
 Split()
 Find()
 Join()
 isalnum(), isalpha(), isdigit(), islower(), isupper(), isspace()
 Isidentifier()
String:- len, lower, upper, strip,
title, capitalize
String:- Replace(), count()
Find(), Split(), join()
String:- strip(), lstrip(), rstrip()
String:- isalnum(), isalpha(), isdigit(), islower(),
isupper(), isspace(), Isidentifier()
Reversing String
 By using the reverse function, you can reverse the string.
For example, if we have string "selenum" and then if you
apply the code for the reverse function as shown below.
enumerate() function
 Contains index and value of item in the list.
 Used to print both index and item in the list.

You might also like