A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Arrange string characters such that lowercase letters should come first using python programming
Q:

Arrange string characters such that lowercase letters should come first using python programming

1

Arrange string characters such that lowercase letters should come first

Given string contains a combination of the lower and upper case letters. Write a program to arrange the characters of a string so that all lowercase letters should come first.

Given:

str1 = PyNaTive

Expected Output:

yaivePNT

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

Iterate each character from a string and check if the current character is the lower or upper case using islower() string function

Solution:

  • Create two lists lower and upper
  • Iterate a string using a for loop
  • In each loop iteration, check if the current character is the lower or upper case using the islower() string function.
  • If a character is the lower case, add it to the lower list, else add it to the upper list
  • to join the lower and upper list using a join() function.
  • convert list to string
  • print the final string
str1 = "PYnAtivE"
print('Original String:', str1)
lower = []
upper = []
for char in str1:
    if char.islower():
        # add lowercase characters to lower list
        lower.append(char)
    else:
        # add uppercase characters to lower list
        upper.append(char)

# Join both list
sorted_str = ''.join(lower + upper)
print('Result:', sorted_str)

 

Explanation:

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now