0% found this document useful (0 votes)
109 views29 pages

Python String Manipulation Guide

Uploaded by

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

Python String Manipulation Guide

Uploaded by

packiya782
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

String manipulation

String
 String is a sequence of characters, which is
enclosed between either single (' ') or double
quotes (" "), python treats both single and double
quotes same.
Traversing a string
 Traversing refers to iterating through
the elements of a string one character
at a time.
String operator
 Concatenation operator +
 Replication operator *
 Membership operator
String operator

Concatenation operator +

 + operator creates a new string by


joining the two operands.
 Eg:
Note:
+ operator have both operands of the same type
either of number type or of string type.
It cannot work with one operand as string and
one as a number
Replication operator *
 * operator- python creates a new string
that is a number of repetitions of the
input string.
Eg:
Note:
• operator have either have both operands of the
number type or one string type and one number type.

It cannot work if both the operand of string type


Membership operator
 in-
› returns true if a character or a substring
exist in the given string otherwise false.
 not in
› returns true if a character or a substring
not exist in the given string otherwise
false.
Comparison operator
 All relational operators are apply to string.
 Comparison is based on the standard character
by character comparison rule for unicode.
 Internally python compares using unicode
values.
Characters Ordinal
values
‘0’ to ‘9’ 48 to 57
‘A’ to ‘Z’ 65 to 90
‘a’ to ‘z’ 97 to 122
 Determining ordinal value
ord(<single-character>)
Eg:

 Determining character from ordinal


value.
chr(<integer>)
Eg:
String slices
 String slice- part of a string containing
some contiguous characters from the
string.
String slices
 Str=‘computer’

 Syntax:
<string variable>[<begin index>:<last
index>]
For any index n, s[:n]+s[n:] give the
orginal string s
 Note: index out of bounds cause error
with strings but slicing a string outside
the bounds does not error.
String functions and methods
 Syntax: <stringobject>.<method
Function Description example
name>()
string.capitalize() Return a copy of the Eg:
string with its first
character capitalized
String.find(sub[,start[,end] Returns the lowest index
]) in the string where the
substring sub is found.
Return -1 if sub is not
found
String.islower() Returns true if the
characters in the strings
are small letter
Otherwise false

String.isupper() Returns true if the


characters in the strings
are capital letter
Otherwise false
Function Description Eg
String. isalpha() Returns true if all
characters in the
strings are alphabetic
Otherwise false
String.isalnum() Returns true if the
characters in the
strings are
alphanumeric
Otherwise false

String.isdigit() Returns true if the


characters in the
strings are digit
Otherwise false

String.isspace()
Returns true if the
characters in the
strings are
whitespaceOtherwise
false
Function Description Eg
String.lower() Return a copy of string
converted to lowercase
String.upper() Return a copy of string
converted to uppercase
String.lstrip([char Return a copy of the
s]) string with leading
characters removed
If no argument is given ,
it removes the leading
whitespaces.

String.rstrip() Return a copy of the


string with trailing
characters removed
If no argument is given ,
it removes the trailing
whitespaces.
len ()
 The len() function gives the length of
the string as the count of characters
contained in it.
 Syntax:

len(<string>)
title() Method
 title() method returns a string where
the first character in every word is
upper case.
 If the word contains a number or a
symbol, the first letter after that will be
converted to upper case.
split() method
 The split() method splits a string into a
list.
 We can specify the separator, default
separator is any whitespace.
partition()
 partition() method searches for a
specified string, and splits the string
into a tuple containing three elements.
replace()
 The replace() method replaces a
specified phrase with another specified
phrase.
Join()
 he join() method takes all items in an
iterable and joins them into one string.
 A string must be specified as the
separator.
startswith()
 the startswith() method returns True if the string
starts with the specified value, otherwise False.
 Syntax:

string.startswith(value, start, end)


Start and end are optional
endswith()
 the endswith() method returns True if the string
ends with the specified value, otherwise False.
 Syntax:

string.endswith(value, start, end)


Start and end are optional

You might also like