Strings
String function in python
Python capitalize() method
converts first character of the string into uppercase without altering the
whole string. It changes the first character only and skips rest of the string
unchanged.
Signature
1. capitalize()
Parameters
No parameter is required.
Return Type
It returns a modified string.
# Python capitalize() function example
# Variable declaration
str = "welcome"
# Calling function
str2 = str.capitalize()
# Displaying result
print("Old value:", str)
print("New value:", str2)
output:
Old value: welcome
New value: Welcome
Python String Casefold() Method
Python Casefold() method
returns a lowercase copy of the string. It is more simillar to lowercase method
except it revomes all case distinctions present in the string.
For example in German, 'β' is equivelent to "ss". Since
it is already in lowercase, lowercase do nothing and prints 'β' whereas
casefold converts it to "ss".
Signature
casefold()
Python String Casefold() Method Example 1
# Python casefold() function example
# Variable declaration
str = "WELCOME"
# Calling function
str2 = str.casefold()
# Displaying result
print("Old value:", str)
print("New value:", str2)
Output:
Old value: WELCOME
New value: welcome
Python String Center() Method
Python center() method
alligns string to the center by filling paddings left and right of the string.
This method takes two parameters, first is a width and second is a fillchar
which is optional. The fillchar is a character which is used to fill left and
right padding of the string.
Signature
1.
center(width[,fillchar])
Parameters
- width (required)
- fillchar (optional)
# Python center() function example
# Variable declaration
str = "Welcome to world"
# Calling function
str2 = str.center(20)
# Displaying result
print("Old value:", str)
print("New value:", str2)
Output:
Old value: Welcome to
world
New value: Welcome to world
Python String Center() Method Example 2
Here, we are providing padding char (optional)
parameter as #. See the example.
# Python center() function example
# Variable declaration
str = "Welcome"
# Calling function
str2 = str.center(20,'#')
# Displaying result
print("Old value:", str)
print("New value:", str2)
Output:
Old value: Welcome
New value: ##Welcome##
Python String Count() Method
It returns the number of occurences of substring in the specified
range. It takes three parameters, first is a substring, second a start index
and third is last index of the range. Start and end both are optional whereas
substring is required.
Signature
1.
count(sub[, start[, end]])
Parameters
- sub (required)
- start (optional)
- end (optional)
# Python count() function example
# Variable declaration
str = "Welcome"
str2 = str.count('e')
# Displaying result
print("occurences:", str2)
Output:
occurences: 2
Python String endswith() Method
Python endswith() method
returns true of the string ends with the specified substring, otherwise returns
false.
Signature
1.
endswith(suffix[, start[, end]])
Parameters
- suffix : a
substring
- start : start
index of a range
- end : last index
of the range
Start and end both parameters are optional.
Return
Type
It returns a boolean value either True or False.
# Python endswith() function example
# Variable declaration
str = "Hello this is javatpoint."
isends = str.endswith(".")
# Displaying result
print(isends)
Output:
True
Python String isalnum() Method
Python isalnum() method
checks whether the all characters of the string is alphanumeric or not. A
character which is either a letter or a number is known as alphanumeric. It
does not allow special chars even spaces.
Signature
isalnum()
Parameters
No parameter is required.
# Python isalnum() function example
# Variable declaration
str = "Welcome"
# Calling function
str2 = str.isalnum()
# Displaying result
print(str2)
Output:
True
Python String isspace() Method
Python isspace() method
is used to check space in the string. It returna true if there are only
whitespace characters in the string. Otherwise it returns false. Space,
newline, and tabs etc are known as whitespace characters and are defined in the
Unicode character database as Other
or Separator.
Signature
1.
isspace()
Parameters
No parameter is required.
Return
It returns either True or False.
# Python isspace() method example
# Variable declaration
str = " " # empty string
# Calling function
str2 = str.isspace()
# Displaying result
print(str2)
Output:
True
Python String isspace() Method Example 3
isspace() method returns true for all
whitespaces like:
- '
' - Space
- '\t'
- Horizontal tab
- '\n'
- Newline
- '\v'
- Vertical tab
- '\f'
- Feed
- '\r'
- Carriage return
# Python isspace() method example
# Variable declaration
str = " " # string contains space
if str.isspace() == True:
print("It contains space")
else:
print("Not space")
str = "ab cd ef \n"
if str.isspace() == True:
print("It contains space")
else:
print("Not space")
str = "\t \r \n"
if str.isspace() == True:
print("It contains space")
else:
print("Not space")
Output:
It contains space
Not space
It contains space
Python String join() Method
Python join() method
is used to concat a string with iterable object. It returns a new string which
is the concatenation of the strings in iterable. It throws an exception
TypeError if iterable contains any non-string value.
It allows various iterables like: List, Tuple, String etc.
Signature
join(iterable)
Parameters
iterable :
iterable object like: List, Tuple, String etc.
# Python join() method example
# Variable declaration
str = ":" # string
list = ['1','2','3'] # iterable
# Calling function
str2 = str.join(list)
# Displaying result
print(str2)
Output:
1:2:3
Python String lstrip() Method
Python lstrip() method
is used to remove all leading characters from the string. It takes a char type
parameter which is optional. If parameter is not provided, it removes all the
leading spaces from the string.
Signature
1.
lstrip([chars])
Parameters
chars (optional) : A
list of chars
Return
It returns a string.
Let's see some examples of lstrip() method to understand it's functionality
Python
String lstrip() Method Example 1
A simple program to understand use of lstrip method. See how it
removes leading spaces.
# Python lstrip() method example
# Variable declaration
str = " Hello "
# Calling function
str2 = str.lstrip()
# Displaying result
print(str)
print(str2)
Output:
Hello
Hello
Python String partition() Method
Python partition() method
splits the string from the string specified in parameter. It splits the string
from at the first occurrence of parameter and
returns a tuple. The tuple contains the three parts before the separator, the
separator itself, and the part after the separator.
It returns an empty tuple having seperator only, if the seperator
not found.
The method signature is given below.
Signature
1.
partition(sep)
Parameters
sep: A string parameter which
separates the string.
Return
It returns a tuple, A 3-Tuple.
Let's see some examples of partition(sep) method to understand
it's functionality.
Python
String partition() Method Example 1
First, let's see simple use of partition method.
# Python partition() method example
# Variable declaration
str = "Java is a programming language"
# Calling function
str2 = str.partition("is")
# Displaying result
print(str2)
# when seperate from the start
str2 = str.partition("Java")
print(str2)
# when seperate is the end
str2 = str.partition("language")
print(str2)
# when seperater is a substring
str2 = str.partition("av")
print(str2)
Python
String partition() Method Example 2
if the separator is not found, It
returns a tuple containing string itself and two empty strings. See the example
below.
# Python partition() method example
# Variable declaration
str = "Java is a programming language"
# Calling function
str2 = str.partition("not")
# Displaying result
print(str2)
Output:
('Java is a programming language', '', '')
Python String replace() Method
Return a copy of the string with all occurrences of
substring old replaced
by new. If
the optional argument count is
given, only the first count occurrences
are replaced.
Signature
replace(old, new[, count])
Parameters
old : An old string
which will be replaced.
new : New string which
will replace the old string.
# Python replace() method example
# Variable declaration
str = "Java is a programming language"
# Calling function
str2 = str.replace("Java","C")
# Displaying result
print("Old String: \n",str)
print("New String: \n",str2)
Output:
Old String:
Java is a programming language
New String:
C is a programming language
Python String rstrip() Method
Python rstrip() method
removes all the trailing characters from the string. It means it removes all
the specified characters from right side of the string. If we don't specify the
parameter, It removes all the whitespaces from the string. This method returns
a string value.
Signature
rstrip([chars])
Parameters
chars: character to be removed
from the string.
Return
It returns string.
# Python rstrip() method example
# Variable declaration
str = "Java and C# "
# Calling function
str2 = str.rstrip()
# Displaying result
print("Old string: ",str)
print("New String: ",str2)
Output:
Old string: Java and C#
New String: Java and C#
Python String swapcase() Method
Python swapcase() method converts case of the string characters
from uppercase to lowercase and vice versa. It does not require any parameter
and returns a string after case conversion.
Signature
1.
swapcase()
Parameters
No Parameter
Return
It returns a string.
Let's see some examples of swapcase() method to understand it's
functionality.
Python
String swapcase() Method Example 1
It is a simple example to convert uppercase to lowercase using the
swapcase() method.
# Python String swapcase() method
# Declaring variable
str = "WELCOME"
# Calling function
str2 = str.swapcase()
# Displaying result
print (str2)
Output:
Welcome
Python String zfill() Method
Python zfill() method fills the string at left with 0 digit to
make a string of length width. It returns a string contains a sign prefix + or
- before the 0 digit.
It returns original length string if the width is less than string
length.
Signature
zfill(width)
# Python zfill(width) method
# Declaring variables
text = "Zfill Example"
# Calling Function
str2 = text.zfill(20)
# Displaying result
print(str2)
Output:
0000000Zfill Example
Comments
Post a Comment