Mrthods and Description
|
||||||||||||||
String capitalize()
Converts first character to
Capital Letter
Return type of this method is String and the method is invoked using a string object and takes no argument or parameter.
str1="i am Indian"
str2=str1.capitalize()
str2 would be 'I am Indian' where
str1 will remain the same ('i am Indian')
For non alphabetic letter at the first position of the string, there will be no change.
str1="# i am Indian"
str2=str1.capitalize()
str2 would be '# i am
Indian'
|
||||||||||||||
String
center()
Pads string with specified
character
Return type of this method is String and the method is invoked using a string object and takes int value and character (optional) as arguments or parameters. This method returns the invoking string centered in width specified in first argument. Padding is done using the specified character passed as second argument, if any character is passed. Default filler character is a space.
str = "Mera Bharat
Mahan"
print str.center(21, '#')
The output would be :
"##Mera Bharat Mahan##"
Again the statement print
str.center(21) would produce the output " Mera Bharat
Mahan "
|
||||||||||||||
String
casefold()
converts to casefolded strings
Return type of this method is string and the method is invoked using a string object. The casefold() method returns a string where all the characters are lower case.
This method is similar to the
lower() method, but the casefold() method is stronger, more aggressive,
meaning that it will convert more characters into lower case.
|
||||||||||||||
String
count()
returns occurrences of substring
in string
Return type of this method is int, the method is invoked using a string object and takes argument (s) or parameters. Syntax of this method is string.count(substring, int start, int end): The second and third arguments are optional. 'string' is the invoking string from where the substring will be searched. 'substring' is the string which will be searched in 'string'. 'start' indicates the index from where search will be performed and 'end' is the end index of searching. If the second and third arguments are not passed, the searching will be performed on the entire string. |
||||||||||||||
boolean endswith()
Checks if String Ends with the
Specified Suffix
Return type of this method is boolean, the method is invoked using a string object and takes argument (s) or parameters. Syntax of this method is boolean endswith(substring, int start, int end): The second and third arguments are optional. 'string' is the invoking string from where the substring will be searched. 'substring' is the string which will be searched in 'string'. 'start' indicates the index from where search will be performed and 'end' is the end index of searching. If the second and third arguments are not passed, the searching will be performed on the entire string. If the search is successful, the method returns true otherwise false. str = "this is testing"; suffix = "testing";
print str.endswith(suffix)
print str.endswith(suffix,20)
suffix = "is";
print str.endswith(suffix, 2, 4)
print str.endswith(suffix, 2, 6)
Output True
True
True
False
|
||||||||||||||
String
expandtabs()
Replaces Tab character With Spaces
The expandtabs() method returns a copy of string with all tab characters '\t' replaced with whitespace characters. Return type of this method is string, the method is invoked using a string object and may or may not take parameter while invoking the method. If the method is invoked without parameter then each '\t' will be replaced by 8 whitespace characters otherwise the number of whitespace characters will be same as the integer value passed as argument.
str = 'abc\txyz'
result = str.expandtabs()
print(result)
Output
abc xyz
|
||||||||||||||
String
encode()
returns encoded string of given
string
|
||||||||||||||
String
find()
Returns the Lowest Index of
Substring
Return type of this method is integer, the method is invoked using a string object and takes a string, start index and end index as parameters. Second and third arguments are optional. The method searches the argument string in the invoking string and if found, it returns the first occurrence index otherwise returns -1. If the 2nd and 3rd arguments are passed, it will search the argument string within the specified indexes. str1 = 'Mata o mata'
result = str1.find('mata')
print("Substring
'mata':", result)
result = str1.find('pita')
print("Substring 'pita
':", result)
Output 7
-1
|
||||||||||||||
String
format()
formats string into nicer output
|
||||||||||||||
String
index()
|
||||||||||||||
String
isalnum()
Checks Alphanumeric Character
Return type of this method is
boolean (true or false). The method is invoked using a string object and
takes no argument. If the invoking string contains alphanumeric characters
only it returns true otherwise false.
str1 = "So234MA"
print(str1.isalnum())
str1 = "So 234MA"
print(str1.isalnum())
str1 = "HiSoma"
print(str1.isalnum())
str1 = "3156"
print(str1.isalnum())
Output True
False
True
True
|
||||||||||||||
String
isalpha()
Checks if All Characters are
Alphabets
Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument. If the invoking string contains alphanumeric characters only it returns true otherwise false.
str1 = "So234MA"
print(str1.isalpha())
str1 = "Soma Das"
print(str1.isalpha())
str1 = "HiSoma"
print(str1.isalpha())
str1 = "3156"
print(str1.isalpha())
Output False
False
True
False
|
||||||||||||||
String
isdecimal()
Checks Decimal Characters
Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument. If the invoking string contains decimal only it returns true otherwise false.
str1 = "So234MA"
print(str1.isdecimal())
str1 = "Soma Das"
print(str1.isdecimal())
str1 = "HiSoma"
print(str1.isdecimal())
str1 = "3156"
print(str1.isdecimal())
Output False
False
False
True
|
||||||||||||||
String
isdigit()
Checks Digit Characters
Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument. If the invoking string contains digits only it returns true otherwise false.
str1 = "So234MA"
print(str1.isdigit())
str1 = "Soma Das"
print(str1.isdigit())
str1 = "HiSoma"
print(str1.isdigit())
str1 = "3156"
print(str1.isdigit())
Output False
False
False
True
|
||||||||||||||
String
isidentifier()
Checks for Valid Identifier
|
||||||||||||||
String
islower()
Checks if all Alphabets in a
String are Lowercase
Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument. If all the alphabets in the invoking string are in lower case only it returns true otherwise false. The string may contain digits, whitespaces.
str1 = "So234MA"
print(str1.islower())
str1 = "Soma Das"
print(str1.islower())
str1 = "hi soma"
print(str1.islower())
Output False
False
True
|
||||||||||||||
String
isnumeric()
Checks Numeric Characters
A numeric character may be Decimal, Digit or Numeric Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument. If all the alphabets in the invoking string are numeric characters, the method would return true otherwise false. |
||||||||||||||
String
isprintable()
Checks Printable Character
Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument. If all the characters in the invoking string are pritable characters, the method would return true otherwise false. Characters that occupies printing space on the screen are known as printable characters like letters and symbols, digits, punctuation and whitespace s = 'We can print space'
print(s)
print(s.isprintable())
s = '\nNew Line Character is printable'
print(s)
print(s.isprintable())
s = '' "
print('\nEmpty string also
printable', s.isprintable())
|
||||||||||||||
String
isspace()
Checks Whitespace Characters
Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument. If all the characters in the invoking string are space, the method would return true otherwise false. s1 = ' \t'
print(s1.isspace())
s1 = ' abcd '
print(s1.isspace())
Output
True
False
|
||||||||||||||
String
istitle()
Checks for Titlecased String
Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument. The method returns true if an uppercase character is followed by a lowercase character or uncased character and if an uppercase character proceeds a character that character must be an uncased character. The method returns false if no uppercase characters, if uppercase follows a lowercase character with no whitespace between the characters, if uppercase follows another uppercase with no whitespace.
Whitespace is a non case
character.
str1 = "Master Code Online"
str1.istitle()
True
str1 = "master code
online"
str1.istitle()
False
|
||||||||||||||
String
isupper()
returns if all characters are
uppercase characters
Return type of this method is boolean (true or false). The method is invoked using a string object and takes no argument. The method returns true if all the characters in the invoking string are in uppercase.
The method returns false if
all the characters in the invoking string are in lowercase.
str1 = "Master Code
Online"
str1.isupper()
False
str1 = "SOMA DAS"
str1.isupper()
True
|
||||||||||||||
String
join()
Returns a Concatenated String
Return type of this method is
string. The method is invoked using a string object and takes a string, list
or tuple as argument.
The join() method provides a
flexible way to concatenate string. It concatenates each element of an
iterable (such as list, string and tuple) to the string and returns the
concatenated string.
The syntax of join() is: string.join(iterable) list = ['1', '2', '3', '4']
sp = ', '
print(sp.join(list))
Output: 1, 2, 3, 4 tup = ('1', '2', '3', '4')
print(sp.join(numTuple))
Output: 1, 2, 3, 4 s1 = 'abc'
s2 = '123'
""" Each character of s2 is concatenated to the front of s1"""
print('s1.join(s2):', s1.join(s2))
Output: s1.join(s2): 1abc2abc3 """ Each character of s1 is concatenated to the front of s2"""
print('s2.join(s1):', s2.join(s1))
Output: s2.join(s1): a123b123c |
||||||||||||||
String
ljust()
returns left-justified string of
given width
Return type of this method is
string. The method is invoked using a string object and takes no argument.
The syntax of ljust() method
is: string.ljust(width[, fillchar])
Here, fillchar is an optional
parameter.
The ljust() method creates 'width'
number of character spaces sets the string within that space in
left-justified order.
If the second argument 'fillchar'
is passed, it also fills the remaining space with the defined character.
Example:
str1 = 'India'
width = 7
fillchar='#'
# print left justified
: print(str1.ljust(width))
Output
str1 = 'India'
width = 7
# print left justified
: print(str1.ljust(width))
Output
|
||||||||||||||
returns right-justified string of
given width
returns right-justified
string of given width
Return type of this
method is string. The method is invoked using a string object and takes no
argument.
The syntax of rjust()
method is: string.rjust(width[, fillchar])
Here, fillchar is an
optional parameter.
The rjust() method
creates 'width' number of character spaces sets the string within that space
in right-justified order.
If the second argument
'fillchar' is passed, it also fills the remaining space with the defined
character.
Example:
str1 = 'India'
width = 7
fillchar='@ '
# print right
justified : print(str1.rjust(width))
Output
str1 = 'India'
width = 7
# print right
justified : print(str1.rjust(width))
Output
|
||||||||||||||
String
lower()
Return type of this method is string. The method is invoked using a string object and takes no argument.
The lower() method returns the lowercased version of the invoking string converting all uppercase alphabets to lowercase. The characters other that alphabets remain unchanged. string = "This is @Dhananjoy Chakraborty" print(string.lower()) Output: this is @dhananjoy chakraborty |
||||||||||||||
String
upper()
Return type of this method is string. The method is invoked using a string object and takes no argument.
The upper() method returns the uppercased version of the invoking string converting all lowercase alphabets to uppercase. The characters other that alphabets remain unchanged. string = "This is @Dhananjoy Chakraborty" print(string.upper()) Output: THIS IS @DHANANJOY CHAKRABORTY |
||||||||||||||
String
swapcase()
Return type of this method is string. The method is invoked using a string object and takes no argument.
The swapcase () method modifies the invoking string by converting all lowercase alphabets into uppercase and vice versa.The characters other that alphabets remain unchanged. string = "This is @Dhananjoy Chakraborty" print(string.swapcase()) Output: tHIS IS @dHANANJOY cHAKRABORTY |
||||||||||||||
String
lstrip()
Return type of this method is string. The method is invoked using a string object and takes string as argument.
The lstrip () method removes all the leading characters specified as argument from the invoking string. If not found, the string remains unchanged. string = "!!!This is @Dhananjoy Chakraborty!!!" print(string.lstrip('!')) Output: This is @Dhananjoy Chakraborty!!! |
||||||||||||||
String
rstrip()
Return type of this method is string. The method is invoked using a string object and takes string as argument.
The rstrip () method removes all the trailing characters specified as argument from the invoking string. If not found, the string remains unchanged. string = "!!!This is @Dhananjoy Chakraborty!!!" print(string.rstrip('!')) Output: !!!This is @Dhananjoy Chakraborty |
||||||||||||||
String
strip()
Return type of this method is string. The method is invoked using a string object and takes string as argument.
The strip () method removes all the leading and trailing characters specified as argument from the invoking string. If not found, the string remains unchanged. string = "!!!This is @Dhananjoy Chakraborty!!!" print(string.strip('!')) Output: This is @Dhananjoy Chakraborty |
||||||||||||||
String
partition()
The method is invoked using a string object and takes string as argument. partition () is a Python String Method which is used to split the given string using the specified separator passed as argument and return a tuple with three arguments. This method will find out the separator from Left Hand side and once it finds the separator, it will return the string before the Separator as Tuple Item 1, Separator itself as Tuple Item 2 and remaining string as Tuple Item 3. If the separator is not found, the method returns the invoking string as Tuple Item 1 and two blank Tuples. string = "Life is fun" print(string.partition('is ')) Output: ('Life ', 'is ', 'fun') print(string.partition('not ')) Output: ('Life is fun', '', '') |
||||||||||||||
String
maketrans()
returns a translation table
|
||||||||||||||
String
rpartition()
Returns a Tuple
|
||||||||||||||
String
translate()
returns mapped charactered string
|
||||||||||||||
String
replace()
Replaces Substring Inside
|
||||||||||||||
String
rfind()
Returns the Highest Index of
Substring
|
||||||||||||||
String
rindex()
Returns Highest Index of Substring
|
||||||||||||||
String
split()
Splits String from Left
|
||||||||||||||
String
rsplit()
Splits String From Right
|
||||||||||||||
String
splitlines()
Splits String at Line Boundaries
|
||||||||||||||
String
startswith()
Checks if String Starts with the
Specified String
|
||||||||||||||
String
title()
Returns a Title Cased String
|
||||||||||||||
String
zfill()
Returns a Copy of The String
Padded With Zeros
|
||||||||||||||
String
format_map()
Formats the String Using
Dictionary
|
||||||||||||||
any()
Checks if any Element of an
Iterable is True
|
||||||||||||||
all()
returns true when all elements in
iterable is true
|
||||||||||||||
ascii()
Returns String Containing
Printable Representation
|
||||||||||||||
bool()
Coverts a Value to Boolean
|
||||||||||||||
bytearray()
returns array of given byte size
|
||||||||||||||
bytes()
returns immutable bytes object
|
||||||||||||||
compile()
Returns a code object
|
||||||||||||||
complex()
Creates a Complex Number
|
||||||||||||||
enumerate()
Returns an Enumerate Object
|
||||||||||||||
filter()
constructs iterator from elements
which are true
|
||||||||||||||
float()
returns floating point number from
number, string
|
||||||||||||||
input()
reads and returns a line of string
|
||||||||||||||
int()
returns integer from a number or
string
|
||||||||||||||
iter()
returns iterator for an object
|
||||||||||||||
len()
Returns Length of an Object
|
||||||||||||||
max()
returns largest element
|
||||||||||||||
min()
returns smallest element
|
||||||||||||||
map()
Applies Function and Returns a
List
|
||||||||||||||
ord()
returns Unicode code point for
Unicode character
|
||||||||||||||
reversed()
returns reversed iterator of a
sequence
|
||||||||||||||
slice()
creates a slice object specified
by range()
|
||||||||||||||
sorted()
returns sorted list from a given
iterable
|
||||||||||||||
sum()
Add items of an Iterable
|
||||||||||||||
zip()
Returns an Iterator of Tuples
|
Wednesday, August 29, 2018
More String Methods in Python Programming Language
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment