A string is a sequence of one or more UNICODE characters, which can include letters, digits, whitespace, and symbols. Strings are immutable, meaning their content cannot be altered once assigned, and various operations such as indexing, slicing, concatenation, and membership can be performed on them. Additionally, Python provides numerous built-in functions for string manipulation, including counting characters, changing case, and replacing substrings.
A string is a sequence of one or more UNICODE characters, which can include letters, digits, whitespace, and symbols. Strings are immutable, meaning their content cannot be altered once assigned, and various operations such as indexing, slicing, concatenation, and membership can be performed on them. Additionally, Python provides numerous built-in functions for string manipulation, including counting characters, changing case, and replacing substrings.
What is Strings?
+ String is basically a sequence which is made up of one or more UNICODE characters
+ Character in string can be any letter, digit, whitespace or any other symbol.
+ String can be created by enclosing one or more characters in single, double or triple quotes.
Examples:
Biter
ceed
aed
peer
INDEXING — Accessing characters in a string
* Individual character in a string can be accessed using indexes
* Indexes are unique numbers assigned to each character in a string to identify them
* Index always begins from 0 and written in square brackets “D
* Index must be an zero, positive or negative integer
* We get Indexérror wher
we give index value out of the range.
Examples:Negative Indexing
+ Python allows negative indexing also,
‘+ Negative indices are used when you want to access string n reverse order
* ‘Starting from the right side, the first character has the index as -1 and the last character (leftmost) has the
index -n where nis length of string.
Teng in sng in Pon
Pommweindces D[if2[s]4[s[e{7fa[s
steng rlefete[rti[e[spolw
Negative es ss [eto[sts|statata
Is string immutable?
‘= Yes, string is immutable data type. The content of string once assigned cannot be altered than.
‘+ Trying to alter string content may lead an error.
String operations
Sting support fellowing operation:
+ Ropetition
a Mabey
+ Slicing
Concatenation
= Concatenation refers to joining two strings
* Plus (‘+ is used as concatenation operator.Repetition
+ Repetition as it name implies repeat the
«+ Asterisk (is used as repetition opera
cos
poses
techtipnowtecht ipnowtechtipno
Membership
‘+ Membership operation refers to checking a string or character is part or subpart ofan existing string or not
+ Python uses ‘in’ and ‘not in’ as membership operator.
* ‘in’ returns true ifthe first sting or character appears as substring in the second string
+ ‘not in’ retumns true ifthe fi
sting or character does not appears as substring in the second string
Slicing
+ Extracting a specific part of string or substring is called slicing
* Subset occurred after slicing contains contiguous elements
* Slicing is done using index range like string[start_index : end_index: step_value]
* End index is always excluded in resultant substring
* Negative index can also be used for slicing.rr
str[2:10]
Deed
Erode es]
Peers toe
ET tEt S|
Does Sa
Prec cren eC nCa ya
#end of stri
cred eC)
ote)
Pca rert mica rec
str[0:10:2]
Dea
Pied
Traversing a String
Traversing a string refers to as
sssing each character of a given string sequer
cd for traversing a st
oat eed
peat)ead
a)
eae eeeeo)
(strfent])
Pere
String Functions
String Fonctions
Function | Description
Count and returns total no of characters ina
te
= string as length
Returns astring with fst letter of ts each
Niet) | wore in uppercase and ret n lowercase
Returns the string with al ts characters in
tower) | Returns
Returns the string with all its characters in et
wore) | uppercase aaa
Count and returns no of tapes a substring
appears na string
county | Svneae
Count subst, start, end)
ifstart and end indexss mar mentions,
searching is done 2¢/03sall chatacters‘iea)
Search and retureyst Oecuri@ace of
substring in given String
Syntax: figa(substr,start-end)
if startand end index'ls not mentioned
searching Bone across all characters
indent)
atte his find) but raises an exception if the
Substring isnot present in the given string
‘endswith()
Returns True f the given string ends with
the substring supplied.
startswith()
Returns True the given string starts with
the substring supplied
\salnum()
Returns True if characters of given string are
either alphabet or numbers.
if string is empty or includes whitespace or
special symbols, it veturn False,
‘saipna()
Returna Troe W characters of given string are
only alphabets
‘saigitg)
Returns True of characters of gen string
are only numbers
‘slower()
Returns True if string is not empty and
contains ail lowercase characters or at least
‘one character in lowercase.
‘suppert)
Returns True if string is not empty and
contains all uppercase characters or atleast
‘one character in uppercase
‘sspace()
Returns Troe W sting not emply and al
‘characters are white space (blank, tab,
newline, carriage return)
{strip()
Returns the string after removing the spaces,
‘only on the left of the string
rstrip()
‘Returns the string after remioyingthe sBaces
‘only on the right of the string
strip()
Returns the string after removing the spaces
both on the left and the'ightiof the stringReplaces al occrféncls6f old string with
the new string.
Syntax
Replace(Sldstr)pewsts)
fold)
étuens'a string in which the characters in
‘the String have been joined by a separator
parttion()
Webyeaks the given string at the fst
‘occurrence of the substring into three parts
as: Substring before the separator,
Separator, ater separator
Af substring isnot found, then aso itretums
three strings as One complete string, Two
empty string
spl)
Separate given string into list of words
ended with specified substring
Space is considered as separator incase of
substring is not spectied