Showing posts with label strings. Show all posts
Showing posts with label strings. Show all posts

Saturday, March 10, 2018

String isdecimal() Method


String isdecimal() Method in Python 3


The isdecimal() method checks whether the string consists of only decimal characters.This method are present only on unicode objects.
Note: Unlike in Python 2, all strings are represented as Unicode in Python 3. Given Below is an example illustrating it.

Syntax
str.isdecimal()

Return Value
This method returns true if all the characters in the string are decimal, false otherwise.
Example
#!/usr/bin/python3
str = "this2016"
print (str.isdecimal())
str = "23443434"
print (str.isdecimal())

Result
False
True

String zfill() Method


String zfill() Method in Python 3


The zfill() method pads string on the left with zeros to fill width.

Syntax
str.zfill(width)

Parameters
width - This is final width of the string. This is the width which we would get after filling zeros.
Return Value
This method returns padded string.
Example
#!/usr/bin/python3
str = "this is string example....wow!!!"
print ("str.zfill : ",str.zfill(40))
print ("str.zfill : ",str.zfill(50))

Result
str.zfill : 00000000this is string example....wow!!!
str.zfill : 000000000000000000this is string example....wow!!!

String upper() Method


String upper() Method in Python 3


The upper() method returns a copy of the string in which all case-based characters have been uppercased.

Syntax
str.upper()

Return Value
This method returns a copy of the string in which all case-based characters have been uppercased.
Example
#!/usr/bin/python3
str = "this is string example....wow!!!"
print ("str.upper : ",str.upper())

Result
str.upper : THIS IS STRING EXAMPLE....WOW!!!

String translate() Method


String translate() Method in Python 3


The method translate() returns a copy of the string in which all the characters have been translated using table (constructed with the maketrans() function in the string module),optionally deleting all characters found in the string deletechars.

Syntax
str.translate(table[, deletechars]);

Parameters
· table - You can use the maketrans() helper function in the string module to create a translation table.
· deletechars - The list of characters to be removed from the source string.

Return Value
This method returns a translated copy of the string.
Example
The following example shows the usage of translate() method. Under this, every vowel in a string is replaced by its vowel position.

#!/usr/bin/python3
from string import maketrans # Required to call maketrans function.
intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)
str = "this is string example....wow!!!";
print (str.translate(trantab))


Result
th3s 3s str3ng 2x1mpl2....w4w!!!

Following is the example to delete 'x' and 'm' characters from the string-
#!/usr/bin/python3
from string import maketrans # Required to call maketrans function.
intab = "aeiouxm"
outtab = "1234512"
trantab = maketrans(intab, outtab)
str = "this is string example....wow!!!";
print (str.translate(trantab))



Result
th3s 3s str3ng 21pl2....w4w!!!

String title() Method


String title() Method in Python 3


The title() method returns a copy of the string in which first characters of all the words are capitalized.

Syntax
str.title();

Return Value
This method returns a copy of the string in which first characters of all the words are capitalized.

Example
#!/usr/bin/python3
str = "this is string example....wow!!!"
print (str.title())

Result
This Is String Example....Wow!!!

Friday, March 9, 2018

String swapcase() Method


String swapcase() Method in Python 3


The swapcase() method returns a copy of the string in which all the case-based
characters have had their case swapped.

Syntax
str.swapcase();

Return Value
This method returns a copy of the string in which all the case-based characters have had their case swapped.

Example
#!/usr/bin/python3
str = "this is string example....wow!!!"
print (str.swapcase())
str = "This Is String Example....WOW!!!"
print (str.swapcase())

Result
THIS IS STRING EXAMPLE....WOW!!!
tHIS iS sTRING eXAMPLE....wow!!!

String strip() Method


String strip() Method in Python 3


The strip() method returns a copy of the string in which all chars have been stripped from the beginning and the end of the string (default whitespace characters).

Syntax
str.strip([chars]);

Parameters
chars - The characters to be removed from beginning or end of the string.

Return Value
This method returns a copy of the string in which all the chars have been stripped from the beginning and the end of the string.

Example
#!/usr/bin/python3
str = "*****this is string example....wow!!!*****"
print (str.strip( '*' ))

Result
this is string example....wow!!!

String startswith() Method


String startswith() Method in Python 3


The startswith() method checks whether the string starts with str, optionally restricting the matching with the given indices start and end.

Syntax
str.startswith(str, beg=0,end=len(string));

Parameters
· str - This is the string to be checked.
· beg - This is the optional parameter to set start index of the matching boundary.
· end - This is the optional parameter to set start index of the matching boundary.

Return Value
This method returns true if found matching with the string otherwise false.

Example
#!/usr/bin/python3
str = "this is string example....wow!!!"
print (str.startswith( 'this' ))
print (str.startswith( 'string', 8 ))
print (str.startswith( 'this', 2, 4 ))

Result
True
True
False

String splitlines() Method


String splitlines() Method in Python 3


The splitlines() method returns a list with all the lines in string, optionally including the line breaks (if num is supplied and is true).


Syntax
str.splitlines( num=string.count('\n'))

Parameters

num - This is any number, if present then it would be assumed that the line breaks need to be included in the lines.
Return Value
This method returns true if found matching with the string otherwise false.

Example
#!/usr/bin/python3
str = "this is \nstring example....\nwow!!!"
print (str.splitlines( ))
Result
['this is ', 'string example....', 'wow!!!']

String split() Method


String split() Method in Python 3


The split() method returns a list of all the words in the string, using str as the separator (splits on all whitespace if left unspecified), optionally limiting the number of splits to num.

Syntax
str.split(str="", num=string.count(str)).

Parameters
· str - This is any delimeter, by default it is space.
· num - this is number of lines to be made
Return Value
This method returns a list of lines.

Example
#!/usr/bin/python3
str = "this is string example....wow!!!"
print (str.split( ))
print (str.split('i',1))
print (str.split('w'))

Result
['this', 'is', 'string', 'example....wow!!!']
['th', 's is string example....wow!!!']
['this is string example....', 'o', '!!!']

String rstrip() Method


String rstrip() Method in Python 3

The rstrip() method returns a copy of the string in which all chars have been stripped from the end of the string (default whitespace characters).

Syntax
str.rstrip([chars])

Parameters
chars - You can supply what chars have to be trimmed.
Return Value
This method returns a copy of the string in which all chars have been stripped from the end of the string (default whitespace characters).


Example
#!/usr/bin/python3
str = " this is string example....wow!!! "
print (str.rstrip())
str = "*****this is string example....wow!!!*****"
print (str.rstrip('*'))

Result
this is string example....wow!!!
*****this is string example....wow!!!

String rjust() Method


String rjust() Method in Python 3


The rjust() method returns the string right justified in a string of length width. Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than len(s).

Syntax
str.rjust(width[, fillchar])

Parameters
· width - This is the string length in total after padding.
· fillchar - This is the filler character, default is a space.
Return Value
This method returns the string right justified in a string of length width. Padding is done using the specified fillchar (default is a space). The original string is returned if the width is less than len(s).

Example
#!/usr/bin/python3
str = "this is string example....wow!!!"
print (str.rjust(50, '*'))

Result
******************this is string example....wow!!!

String rindex() Method


String rindex() Method in Python 3


The rindex() method returns the last index where the substring str is found, or raises an exception if no such index exists, optionally restricting the search to string[beg:end].

Syntax
str.rindex(str, beg=0 end=len(string))

Parameters
· str - This specifies the string to be searched.
· beg - This is the starting index, by default its 0.
· len - This is ending index, by default its equal to the length of the string.

Return Value
This method returns last index if found otherwise raises an exception if str is not found.

Example
#!/usr/bin/python3
str1 = "this is really a string example....wow!!!"
str2 = "is"
print (str1.rindex(str2))
print (str1.rindex(str2,10))

Result
5
Traceback (most recent call last):
File "test.py", line 5, in
print (str1.rindex(str2,10))
ValueError: substring not found

String rfind() Method


String rfind() Method in Python 3


The rfind() method returns the last index where the substring str is found, or -1 if no such index exists, optionally restricting the search to string[beg:end].

Syntax
str.rfind(str, beg=0 end=len(string))

Parameters
· str - This specifies the string to be searched.
· beg - This is the starting index, by default its 0.
· end - This is the ending index, by default its equal to the length of the string.

Return Value
This method returns last index if found and -1 otherwise.
Example
#!/usr/bin/python3
str1 = "this is really a string example....wow!!!"
str2 = "is"
print (str1.rfind(str2))
print (str1.rfind(str2, 0, 10))
print (str1.rfind(str2, 10, 0))
print (str1.find(str2))
print (str1.find(str2, 0, 10))
print (str1.find(str2, 10, 0))

Result
5
5
-1
2
2
-1