converting specific letters to uppercase or lowercase in python

  • Last Update :
  • Techknowledgy :

To capitalize the first letter, use the capitalize() function. This functions converts the first character to uppercase and converts the remaining characters to lowercase. It doesn't take any parameters and returns the copy of the modified string.,To capitalize the first letter of each word use the title() function. This function does not take any parameters and converts the first letter of each word to uppercase and rest of the letters to lowercase and returns the modified copy of the string.,To convert all the letters of the string to uppercase, we can use the upper() function. The function does not take any parameters and returns the copy of modified string with all letters converted to upper-case.,To convert all the letters of the string to lowercase, we can use the lower() function. This function does not take any parameters and converts all the letters to lowercase.

Example:

s = "hello openGeNus"
t = s.capitalize()
print(t)
s = "hello openGeNus"
t = s.capitalize()
print(t)
'Hello opengenus'
s = "hello openGeNus"
t = s.upper()
print(t)
'HELLO OPENGENUS'
s = "hello openGeNus"
t = s.title()
print(t)
'Hello Opengenus'

Let's go through some sample code

in = input("Type 'Yes' to continue or 'No' to abort: ")
if in.lower() == "yes":
   print("task continued")
#...more functions
else:
   print("task aborted")
# user did not type Yes

Suggestion : 2

If you're just looking to replace specific letters:

>>> s = "test" >>>
   s.replace("t", "T")
'TesT'

There is one obvious solution, slice the string and upper the parts you want:

test = 'test'
test = test[0].upper() + test[1: -1] + test[-1].upper()
import re
input = 'test'
change_to_upper = 't'
input = re.sub(change_to_upper, change_to_upper.upper(), input)

You could use the str.translate() method:

import string

# Letters that should be upper - cased
letters = "tzqryp"
table = string.maketrans(letters, letters.upper())

word = "test"
print word.translate(table)

As a general way to replace all of a letter with something else

>>> swaps = {
      't': 'T',
      'd': 'D'
   } >>>
   ''.join(swaps.get(i, i) for i in 'dictionary')
'DicTionary'

For python2:

>>> from string
import maketrans
   >>>
   "test".translate(maketrans("bfty", "BFTY"))
'TesT'

And for python3:

>>> "test".translate(str.maketrans("bfty", "BFTY"))
'TesT'

Suggestion : 3

isupper(), islower(), lower(), upper() in Python and their applications,In this article, we will discuss about isupper(), islower(), upper(), and lower() functions in Python. These methods are built-in methods used for handling strings. Before studying them in detail let’s get a basic idea about them.,In Python, upper() is a built-in method used for string handling. The upper() method returns the uppercased string from the given string. It converts all lowercase characters to uppercase. If no lowercase characters exist, it returns the original string. ,In Python, lower() is a built-in method used for string handling. The lower() method returns the lowercased string from the given string. It converts all uppercase characters to lowercase. If no uppercase characters exist, it returns the original string. 

This function is used to check if the argument contains any uppercase characters such as :

Input: string = 'GEEKSFORGEEKS'
Output: True

Input: string = 'GeeksforGeeks'
Output: False

Output:

True
False

This function is used to check if the argument contains any lowercase characters such as :

Input: string = 'geeksforgeeks'
Output: True

Input: string = 'GeeksforGeeks'
Output: False
  1. It does not take any arguments, Therefore, It returns an error if a parameter is passed.
  2. Digits and symbols return are returned as it is, Only a lowercase letter is returned after converting to uppercase.
Input: string = 'geeksforgeeks'
Output: GEEKSFORGEEKS

Input: string = 'My name is ayush'
Output: MY NAME IS AYUSH

Given a string, the task is to write a Python Program to count a number of uppercase letters, lowercase letters, and spaces in a string and toggle case the given string (convert lowercase to uppercase and vice versa).

Input: string = 'GeeksforGeeks is a computer Science portal for Geeks'
Output: Uppercase - 4
Lowercase - 41
spaces - 7
gEEKSFORGEEKS IS A COMPUTER sCIENCE PORTAL FOR gEEKS

Input: string = 'My name is Ayush'
Output: Uppercase - 2
Lowercase - 11
spaces - 3
mY NAME IS aYUSH

Suggestion : 4

The .upper() and .lower() string methods are self-explanatory. Performing the .upper() method on a string converts all of the characters to uppercase, whereas the lower() method converts all of the characters to lowercase.,There are several built-in methods that allow us to easily make modifications to strings in Python. In this tutorial we will cover the .upper(), .lower(), .count(), .find(), .replace() and str() methods.,The .count() method adds up the number of times a character or sequence of characters appears in a string. For example:,We search for a specific character or characters in a string with the .find() method.

>>> s = “This string contains forty - two characters.” >>>
   len(s)
42
>>> s = “Whereof one cannot speak, thereof one must be silent.” >>>
   s 'Whereof one cannot speak, thereof one must be silent.' >>>
   s.upper()
'WHEREOF ONE CANNOT SPEAK, THEREOF ONE MUST BE SILENT.' >>>
s.lower()
'whereof one cannot speak, thereof one must be silent.'
>>> s = "That that is is that that is not is not is that it it is" >>>
   s.count("t")
13
>>> s = s.lower() >>>
   s.count("t")
14
s = "James while John had had had had had had had had had had had a better effect on the teacher" >>>
   s.count("had")
11
s = "On the other hand, you have different fingers." >>>
   s.find("hand")
13

Suggestion : 5

upper() method returns the uppercase string from the given string. It converts all lowercase characters to uppercase.,The upper() method converts all lowercase characters in a string into uppercase characters and returns it.,If no lowercase characters exist, it returns the original string.,Note: If you want to convert to lowercase string, use lower(). You can also use swapcase() to swap between lowercase to uppercase.

Example

message = 'python is fun'

# convert message to uppercase
print(message.upper())

# Output: PYTHON IS FUN

Example

message = 'python is fun'

# convert message to uppercase
print(message.upper())

# Output: PYTHON IS FUN

The syntax of upper() method is:

string.upper()

Example 1: Convert a string to uppercase

# example string
string = "this should be uppercase!"
print(string.upper())

# string with numbers
# all alphabets should be lowercase
string = "Th!s Sh0uLd B3 uPp3rCas3!"
print(string.upper())

Example 2: How upper() is used in a program?

# first string
firstString = "python is awesome!"

# second string
secondString = "PyThOn Is AwEsOmE!"

if (firstString.upper() == secondString.upper()):
   print("The strings are same.")
else:
   print("The strings are not same.")

Suggestion : 6

To convert a Python string to lowercase, use the built-in lower() method of a string. To convert a Python string to uppercase, use the built-in upper() method.,To convert each lowercase letter to uppercase and each uppercase letter to lowercase, use the swapcase() method.,If you want to test if a string is in uppercase or lowercase in Python, use the built-in isupper() and islower() methods:,Today you learned how to convert strings to lowercase and to uppercase in Python. Also, you saw some examples of how to apply other casings too.

1._
"Hello, world".upper() # HELLO WORLD "HELLO, WORLD".lower() # hello world

For example:

"Hello, world".islower() # False "HELLO, WORLD".isupper() # True

Here is an example:

"hello, world".capitalize() # Hello, world