- Python program to Uppercase selective indices
- Methods Used
- Example
- Input
- Output
- Method 1: Using For Loop and upper() function
- Algorithm (Steps)
- Example
- Output
- Method 2: Using List Comprehension
- List Comprehension
- Algorithm (Steps)
- Example
- Output
- Method 3: Using index() & join() functions
- index() function
- Syntax
- Algorithm (Steps)
- Example
- Output
- Conclusion
- How to capitalize elements in list Python | Example code
- Example capitalize elements in list Python
- Using an upper() method with Loop
- Use a list comprehension
- Using a map with lambda
- How to Convert List to Uppercase in Python?
- Hardik Savani
- We are Recommending you
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
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.
How to capitalize elements in list Python | Example code
Use string upper() method to convert every element in a list of strings into uppercase (capitalize) in Python code.
Example capitalize elements in list Python
Simple python example code.
Using an upper() method with Loop
Use a Python for loop to iterate through each item in a given list of strings by index. Then use the string upper() method on each string and reassign it to the corresponding index in the list for that string.
list1 = ["a", "b", "c"] for i in range(len(list1)): list1[i] = list1[i].upper() print(list1)
Use a list comprehension
Example code
list1 = ["a", "b", "c"] list1 = [each_string.upper() for each_string in list1] print(list1)
Using a map with lambda
list1 = map(lambda x: x.upper(), ["a", "b", "c"]) print(list(list1))
Do comment if you have any doubts and suggestions on this Python list code.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
How to Convert List to Uppercase in Python?
This post will give you example of python convert list values to uppercase. I would like to show you convert list to uppercase python. let’s discuss about convert list elements to uppercase python. you will learn convert list data to uppercase python.
There are many ways to convert list elements to uppercase in python. i will give you two examples using for loop with upper() to convert list data to uppercase. so let’s see the below examples.
You can use these examples with python3 (Python 3) version.
let’s see below a simple example with output:
myList = ['One', 'two', 'Three'] # Convert List Value into uppercase for i in range(len(myList)): myList[i] = myList[i].upper() print(myList)
myList = ['One', 'two', 'Three'] # Convert List Value into uppercase newList = [x.upper() for x in myList] print(newList)
Hardik Savani
I’m a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Convert List to Lowercase in Python?
- How to Convert List into String in Python?
- Python Split String into List of Characters Example
- How to Convert String into List in Python?
- How to Remove Duplicate Values from List in Python?
- How to Get Unique Elements from List in Python?
- How to Remove null Values from the List in Python?
- Python Remove Empty String from List Example
- Python Get Second Last Element of List Example
- How to Get the Last Element of a List in Python?
- How to Get First Element of List in Python?
- How to Remove Last Element from List in Python?
- How to Remove First Element from List in Python?