Python program to Uppercase selective indices


In this article, we will learn a python program to convert the string elements to uppercase for the given selective indices.

Methods Used

The following are the various methods to accomplish this task −

  • Using For Loop and upper() function

  • Using List Comprehension

  • Using index() & join() functions

Example

Assume we have taken an input string and a list containing index values. We will now convert the characters of an input string to uppercase at those given indices and print the resultant string.

Input

inputString = 'hello tutorialspoint python'
indexList = [0, 6, 8, 10, 15, 21, 22]

Output

Hello TuToRialsPoint PYthon

In this example, the characters at the input list indices 0, 6, 8, 10, 15, 21, 22 are capitalized in an input string.

Method 1: Using For Loop and upper() function

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a variable to store the input string and print the given input string.

  • Create another variable to store the input indices list.

  • Initialize an empty string for storing the resultant string.

  • Use the for loop to traverse through each character of an input string till its length using the len() function(returns the number of items in an object).

  • Use the if conditional statement to check whether the current index is in the input indices list with the “in” operator.

  • Use the upper() function(converts all the characters into uppercase) to convert that corresponding character of an input string into uppercase.

  • Add the uppercase character to the resultant string using the + operator(string concatenation) if the condition is true.

  • Else add that current character to the resultant string directly without modifying.

  • Print the resultant string after converting the characters of an input string to uppercase at the input indices.

Example

The following program returns a string after converting input string characters to uppercase at the given input indices using the for loop and upper() function –

# input string
inputString = 'hello tutorialspoint python'

# printing input string
print("Input String:", inputString)

# input indices list
indexList = [0, 6, 8, 10, 15, 21, 22]

# initializing empty string for storing resultant string
resultantStr = ''

# travsering till the length of an input string
for i in range(0, len(inputString)):
   
   # checking whether the current index is in the input indices list
   if i in indexList:
      
      # converting that corresponding character of the input string into uppercase
      
      # and adding to the resultant string
      resultantStr += inputString[i].upper()
   else:
      
      # else adding that char to the resultant string directly without modifying
      resultantStr += inputString[i]

# printing resultant string
print("Resultant string after converting chars to uppercase at the input indices:\n", resultantStr)

Output

On executing, the above program will generate the following output –

Input String: hello tutorialspoint python
Resultant string after converting chars to uppercase at the input indices:
Hello TuToRialsPoint PYthon

Method 2: Using List Comprehension

List Comprehension

When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.

join() − join() is a string function in Python that is used to join elements of a sequence that are separated by a string separator.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Using list comprehension traverse through the length of the string and check if this index exists in the index list if so then convert to upper case else add the same character.

  • Convert the result list to a string using the join() function by passing the delimiter as a null string.

Example

The following program returns a string after converting input string characters to uppercase at the given input indices using list comprehension –

# input string
inputString = 'hello tutorialspoint python'

# printing input string
print("Input String:", inputString)

# input indices list
indexList = [0, 6, 8, 10, 15, 21, 22]

# Using list comprehension to achieve the same in the oneliner
resultantList = [inputString[i].upper() if i in indexList else inputString[i] for i in range(0, len(inputString))]

# Converting the result list to a string using the join() function.
resultantStr = ''.join(resultantList)

# printing resultant string
print("Resultant string after converting chars to uppercase at input indices:\n", resultantStr)

Output

Input String: hello tutorialspoint python
Resultant string after converting chars to uppercase at input indices:
Hello TuToRialsPoint PYthon

Method 3: Using index() & join() functions

index() function

The position at the first occurrence of the provided value is returned by the index() function.

Syntax

list.index(element)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Create a variable to store all the lowercase alphabets as a string.

  • Create a variable to store all the uppercase alphabets as a string.

  • Use the list() function to convert the input string into a list(list of characters).

  • Use the for loop to traverse through each index of the input indices list.

  • Get the index of the current character in lower case alphabets string using the index() function and get the corresponding index upper case character from the uppercase alphabets string.

  • Convert the above resultant list into a string again using the join() function.

Example

The following program returns a string after converting input string characters to uppercase at the given input indices using index() & join() functions –

# input string
inputString = 'hello tutorialspoint python'

# printing input string
print("Input String:", inputString)

# input indices list
indexList = [0, 6, 8, 10, 15, 21, 22]

# initializing all the lowercase alphabets
lowercase_alpha = "abcdefghijklmnopqrstuvwxyz"

# initializing all the uppercase alphabets
uppercase_alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

# converting input string into a list (list of chars)
inputString = list(inputString)

# traversing through each index of input indices list
for i in indexList:
   
   # Converting index to upper case
   inputString[i] = uppercase_alpha[lowercase_alpha.index(inputString[i])]

# Joining the list to convert it to a string
inputString = "".join(inputString)

# printing resultant string
print("Resultant string after converting chars to uppercase at the input indices:\n", inputString)

Output

Input String: hello tutorialspoint python
Resultant string after converting chars to uppercase at the input indices:
Hello TuToRialsPoint PYthon

Conclusion

For the given selective indices, we have studied three different ways in this article to change the string elements to uppercase.Additionally, we learned how to use the index() function to find the index of any element or item in an iterable, including a list, tuple, set, etc.

Updated on: 27-Jan-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements