0% found this document useful (0 votes)
13 views38 pages

Python String Functions Guide

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)
13 views38 pages

Python String Functions Guide

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

String Build in Functions

Python String
• The Python string data type is a sequence made up of one or more
individual characters that could consist of letters, numbers, whitespace
characters, or symbols. Because a string is a sequence, it can be
accessed in the same ways that other sequence-based data types are,
through indexing and slicing.
Accessing values in String
A=‘Tech Wyvern!.’ T E C H W Y V E R N ! .
Indexing 0 1 2 3 4 5 6 7 8 9 10 11 12

Print(a[4])
T E C H W Y V E R N ! .
Print(a[-3]) -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Slicing
Print(a[0:4])
Print(a[-4:-1])
Accessing values in String by stride or step
>>> a="Tech Wyvern!"
>>> a[Link]
'Te'
>>> #start,stop,step
>>> a[Link]
'Tc '
>>> a[Link]
'Th'
>>>
capitalize() and center()
• capitalize()  to make 1st letter in caps
• center()  to make the string in centre
and fill the remaining indexes with
some other elements
upper(),lower() and swapcase()
• upper ()  used to convert to upper case
• lower ()  used to convert to lower case
• swapcase()  used to convert lowercase
to uppercase and uppercase to lowercase
casefold()
• Like lower case fold also converts to Lower case, but it will help to
convert the words which is not comes under normal Alpha ("der
Fluß“)
encode()
• Which is used to remove the some
words which even can’t able to
convert by case fold.
• In this Example ‘ö’ is can’t able to
convert by case fold but encode
can able the remove the word.
startswith() and endswith()
• startswith()  which is used to
check either startwith the
particular letter / Sequence or not
• endswith()  which is used to
check either its endswith the
particular letter/sequence
Count()
• To check how many times the elements presented in the sequence
expandtabs()
• Used to increase the Tab Spaces ‘\t’ Now its not working
find() and rfind()
• To find the index position of element
if output is -1 then the element is not
available in sequence
• find  works left to right
• rfind works Right to Left
index() and rindex()
• index  Which is used to found the
index position of one letter or
sequence starts from left
• rindex Which is used to found
the index position of one letter or
sequence starts from right
Format
• Which is used to make the default template and with different values
formatmap()
• Used to deal with dictionaries, difference is, the [Link](**mapping)
copies the dict whereas str.format_map(mapping) makes a new
dictionary during method call. This can be useful if you are working
with a dict subclass.
Split() and Splitlines()
• [Link](separator, max)
Splits the string at the specified separator, and returns
a list

• [Link](keeplinebreaks)
The splitlines() method splits a string into a list. The
splitting is done at line breaks
title()
[Link]()  The title() method returns a string where the first
character in every word is upper case.
zfill()
[Link](len)  zfill() method adds zeros (0) at the beginning of the
string, until it reaches the specified length.
ljust() and rjust()
[Link](width[, fillchar]) string ljust() method returns a left-justified string of a given minimum width.
[Link](width[, fillchar])  string rjust() method returns a right-justified string of a given minimum width
Strip,lstrip and rstrip
• strip Leading Characters Removed
• lstrip  Left Leading Characters Removed
• rstrip  Right Leading Characters Removed
partition() and rpartition()
• partition to split string to tuple with
three strings
• rpartition work based on right
join()
Join used to convert string to List
replace()
• Used to replace one string with another one
isalnum(), is upper(), and islower()
• AlphaNumeric:A character that
is either a letter or a number.
• isupper() if all the character
are in upper case
• islower()  all the character are
in lower case
Isalpha()
isalpha()True if all characters in the string are alphabets (can be both
lowercase and uppercase). False if at least one character is not
alphabet.
isdecimal()
[Link]() True if all characters in the string are
decimal characters, False if at least one character is not
decimal character.
[Link]()
[Link]()returns True if all characters in a string are digits. If
not, it returns False.
isidentifier()
isidentifier()  True if the string is a valid identifier in Python. If not, it returns
False.
isnumeric()
isnumeric() True if all characters in a string are numeric characters.
isprintable()
isprintable()  True if all characters in the string are printable or the
string is empty. If not, it returns False.
istitle()
istitle() if the string is a titlecased string. If not, it returns False
String Operators

Concatenation operator ‘+’


Repetition of string using * operator
Repetition of string using * operator
Range of slice
String operator ‘in’
String operator ‘not in’
Concatenation operator ‘+’

var1 = 'Welcome '


var2 = 'John'
var3 = var1 + var2
print ("'welcome ' + 'John' = " + var3)
Repetition of string using * operator

var1 = 'Welcome '


var2 = var1*6
print ("'welcome ' * 6 = " + var2)
Retrieving character from a given index of
string
var1 = 'Welcome '
print ("The second character in string 'welcome ' is " + var1[1])
Range of slice

var1 = 'Welcome ‘
print ("The second to fourth characters in string 'welcome ' are " +
var1[1:5])
String operator ‘in’
var1 = 'Welcome’
if 'e' in var1:
print ("'e' exists in the word 'welcome’”)
else:
print ("'e' does not exist in the word 'welcome'")
Not in
var1 = 'Welcome'
if 'e' not in var1:
print ("'e' does not exist in the word 'welcome'")
else:
print ("'e' exists in the word 'welcome'")

You might also like