Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Python String Capitalize() Method Example 1

1. # Python capitalize() function example  
2. # Variable declaration  
3. str = "javatpoint"  
4. # Calling function  
5. str2 = str.capitalize()  
6. # Displaying result  
7. print("Old value:", str)  
8. print("New value:", str2)  

Python String Capitalize() Method Example 2


What if first character is a digit or non-alphabet character? This method does not alter character
if it is other than a string character.

1. # Python capitalize() function example  
2. # Variable declaration  
3. str = "#javatpoint"  
4. # Calling function  
5. str2 = str.capitalize()  
6. # Displaying result  
7. print("Old value:", str)  
8. print("New value:", str2)  
9. print("--------digit---------")  
10. str3 = "1-javatpoint"  
11. str4 = str3.capitalize()  
12. print("Old value:", str3)  
13. print("New value:", str4)  

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
1. casefold()  
Parameters
No parameter is required.

Return Type
It returns lowercase string.

Python Version
This function was introduced in Python 3.3.

Python String Casefold() Method Example 1


1. # Python casefold() function example  
2. # Variable declaration  
3. str = "JAVATPOINT"  
4. # Calling function  
5. str2 = str.casefold()  
6. # Displaying result  
7. print("Old value:", str)  
8. print("New value:", str2)  

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)

Return Type
It returns modified string.
Python String Center() Method Example 1:default fill char
Here, we did not pass second parameter. By default it takes spaces.

1. # Python center() function example  
2. # Variable declaration  
3. str = "Hello Javatpoint"  
4. # Calling function  
5. str2 = str.center(20)  
6. # Displaying result  
7. print("Old value:", str)  
8. print("New value:", str2)  

Python String rsplit() Method


Python rsplit() method seperates the string and returns a list. It splits from the right using
seperator as a delimiter. If seperator is not specified, any whitespace string is a separator. This
method does same as split() except splitting from the right which is described in detail below.

Note: if separator is not given, whitespace is treated as separator.

Signature
1. rsplit(sep=None,maxsplit=-1)  

Parameters
sep: A string parameter acts as a seperator.

maxsplit: number of times split perfomed.

Return
It returns a comma separated list.

Let's see some examples of rsplit() method to understand it's functionality.

Python String rsplit() Method Example 1


This is a simple example to understand the usage of rsplit() method.

1. # Python rsplit() method example  
2. # Variable declaration  
3. str = "Java is a programming language"  
4. # Calling function  
5. str2 = str.rsplit()  
6. # Displaying result  
7. print(str2)  

Python String rsplit() Method Example 2


Let's pass a parameter separator to the method, see the example.

1. # Python rsplit() method example  
2. # Variable declaration  
3. str = "Java is a programming language"  
4. # Calling function  
5. str2 = str.rsplit('Java')  
6. # Displaying result  
7. print(str2)  

Python String rsplit() Method Example 3


The string is splited each time when a is occurred. See the example below.

1. # Python rsplit() method example  
2. # Variable declaration  
3. str = "Java is a programming language"  
4. # Calling function  
5. str2 = str.rsplit('a')  
6. # Displaying result  
7. print(str2)  

Python String isdecimal() Method


Python isdecimal() method checks whether all the characters in the string are decimal or not.
Decimal characters are those have base 10.

This method returns boolean either true or false.

Signature
1. isdecimal()  

Parameters
No parameter is required.
Return
It returns either True or False.

Let's see some examples of isdecimal() method to understand it's functionalities.

Python String isdecimal() Method Example 1


A simple example of isdecimal() method to check whether the string contains decimal value.

1. # Python isdecimal() method example  
2. # Variable declaration  
3. str = "Javatpoint"  
4. # Calling function  
5. str2 = str.isdecimal()  
6. # Displaying result  
7. print(str2)  

Python String isdecimal() Method Example 2


Let's try to check floating value and see the outcome. It will return False if string is not decimal.

1. # Python isdecimal() method example  
2. # Variable declaration  
3. str = "123"     # True  
4. str3 = "2.50"   # False  
5. # Calling function  
6. str2 = str.isdecimal()  
7. str4 = str3.isdecimal()  
8. # Displaying result  
9. print(str2)  
10. print(str4)  

Python String isdecimal() Method Example 3


Here, we are checking special chars also.

1. # Python isdecimal() method example  
2. # Variable declaration  
3. str = "123"     # True  
4. str3 = "@#$"   # False  
5. # Calling function  
6. str2 = str.isdecimal()  
7. str4 = str3.isdecimal()  
8. # Displaying result  
9. print(str2)  
10. print(str4)  

Python String isupper() Method


Python isupper() method returns True if all characters in the string are in uppercase. It returns
False if characters are not in uppercase.

Signature
1. isupper()  

Parameters
No parameter is required.

Return
It returns either True or False.

Let's see some examples of isupper() method to understand it's functionalities.

Python String isupper() Method Example 1


1. # Python isupper() method example  
2. # Variable declaration  
3. str = "WELCOME TO JAVATPOINT"  
4. # Calling function  
5. str2 = str.isupper()  
6. # Displaying result  
7. print(str2)  

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.

1. # Python String swapcase() method  
2. # Declaring variable  
3. str = "HELLO JAVATPOINT"  
4. # Calling function  
5. str2 = str.swapcase()  
6. # Displaying result  
7. print (str2)  

Python String swapcase() Method Example 2


This example describes conversion from lowercase to uppercase.

1. # Python String swapcase() method  
2. # Declaring variable  
3. str = "hello javatpoint"  
4. # Calling function  
5. str2 = str.swapcase()  
6. # Displaying result  
7. print (str2)  

Python String swapcase() Method Example 3


If the string is in camelcase, the output of this method also would be in camelcase. All the
lowercase will be converted to uppercase and vice versa.

1. # Python String swapcase() method  
2. # Declaring variable  
3. str = "Hello JavTpoint"  
4. # Calling function  
5. str2 = str.swapcase()  
6. # Displaying result  
7. print (str2)  
Task

Write two programs to illustrate Python built in functions for Lists and Dictionary Manipulation

You might also like