- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Add K between case shifts
In python we can use various internal functions like isupper(), islower() and and join() that can help us adding k symbol between case shifts. The following example will help you understand what is case shift and how we can add k symbol between them.
Example
Assume we have taken an input string and K. We will now add the input k symbol between Case shifts using the above methods. Case shift simply means the words shifting between uppercase and lowercase.
Input
inputString = tUtoRialsPoInt k = ‘#
Output
Resultant String after adding # between caseshifts − t#U#to#R#ials#P#o#I#nt
Using for loop, isupper() and islower() functions
isupper() method − If all of the characters are in upper case, the isupper() method returns True; or else, it returns False.
Syntax
str.isupper()
islower() method − If all of the characters are in lower case, the islower() method returns True; or else, it returns False.
Syntax
str.islower()
Note
Only alphabet characters are checked, not numbers, symbols, or spaces.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task –.
Create a variable to store the input string.
Print the input string.
Create another variable to store the input symbol/character to be replaced between case shifts.
Initialize an empty input string to store the resultant string.
Use the for loop to traverse through the length of the input string using the len() function.
len() function − The number of items in an object is returned by the len() method.
Use the if conditional statement to check whether the current character is upper and its next character is lower or the current character is lower and its next character is upper using islower() and isupper() functions.
Add the current character followed by the input symbol to the resultant string if the condition is true.
Else add the current character to the resultant string directly.
Concatenate the last element of the string using the ‘+’ operator.
Print the resultant string.
Example
The following program returns a string after adding a specified k symbol between case shifts in an input string using for loop, isupper() and islower() functions –
# input string inputString = 'tUtoRialsPoInt' # printing input string print("Input String: ", inputString) # input symbol/char to be replaced with k k = '#' # resultant string resultanatStr = "" # traversing through the length of the input string for c in range(0, len(inputString) - 1): # Checking consecutive string characters for case shift if inputString[c].isupper() and inputString[c + 1].islower() or inputString[c].islower() and inputString[c + 1].isupper(): # adding the current character followed by the input symbol to the # resultant string if the condition is true resultanatStr = resultanatStr + inputString[c] + k else: # else adding current character to the resultant string directly resultanatStr = resultanatStr + inputString[c] # Concatenating the last element of the string resultanatStr = resultanatStr + inputString[-1] # printing the resultant string print("Resultant String after adding # between caseshifts: ", resultanatStr)
Output
Input String: tUtoRialsPoInt Resultant String after adding # between caseshifts: t#U#to#R#ials#P#o#I#nt
Method 2: Using 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. This function connects sequence elements to convert to a string.
Example
The following program returns a string after adding a specified k symbol between case shifts in an input string using list comprehension, isupper() and islower() functions –
# input string inputString = 'tUtoRialsPoInt' # printing input string print("Input String: ", inputString) # input symbol/character to be replaced with k k = '#' # Add the character with k if there is case shifts # else add input character using the list comprehension # Join these list elements as a string using the join() function resultanatStr = ''.join([inputString[c] + k if (inputString[c].isupper() and inputString[c + 1].islower()) or (inputString[c].islower() and inputString[c + 1].isupper()) else inputString[c] for c in range(0, len(inputString) - 1)]) # Concatenating the last element of the string resultanatStr = resultanatStr + inputString[-1] # printing the resultant string print("Resultant String after adding # between caseshifts: ", resultanatStr)
Output
On executing, the above program will generate the following output –
Input String: tUtoRialsPoInt Resultant String after adding # between caseshifts: t#U#to#R#ials#P#o#I#nt
Conclusion
In this article, we have learned how to Add K between case shifts using 2 different approaches. Additionally, we learned how to use the if conditional statements, lower(), and upper() function to check for case shifts. Finally, we learned how to connect or concatenate two strings using the string concatenation operator (+).
To Continue Learning Please Login
Login with Google