1
str_=raw_input('Enter:')
for i,k in enumerate(str_):
    if str_[i] != str_[i].upper():
        v=str(str_[i].upper())
        print v,  
    else:
        c= str(str_[i].lower())
        print c,

This code helps swapping the cases My output is right but i'm getting spaces in between i tried all possible ways to remove the spaces like re.sub,strip(\n),remove(' ','' ) still my output is like this

  >>>Enter:juNgLe
  J U n G l E

where i want my output:JUnGlE

Automa Sha
  • 51
  • 6

6 Answers6

1

You can easily import the print function from python3 which is better than python 2x's print statment

from __future__ import print_function

str_=raw_input('Enter:')
for i,k in enumerate(str_):
    if str_[i] != str_[i].upper():
        v=str(str_[i].upper())
        print(v, end='')
    else:
        c= str(str_[i].lower())
        print(c, end='')
danidee
  • 9,298
  • 2
  • 35
  • 55
1
str_=raw_input('Enter:')

response="";

for i,k in enumerate(str_):
    if str_[i] != str_[i].upper():
        v=str(str_[i].upper())
        response=response+v
    else:
        c= str(str_[i].lower())
        response=response+c
print response

try to add all the letters to new string variable and then print that new variable

yossi
  • 31
  • 2
1

For this solution you'll need to use print from Python 3. This should work:

from __future__ import print_function

str_=raw_input('Enter:')
for i,k in enumerate(str_):
    if str_[i] != str_[i].upper():
        v=str(str_[i].upper())
        print(v, end="")
    else:
        c= str(str_[i].lower())
        print(c, end="")
Stan_MD
  • 1,011
  • 9
  • 8
0

Use sys.stdout.write(c) instead of print. See this

Community
  • 1
  • 1
ZeenaZeek
  • 271
  • 3
  • 5
  • 16
0

You can make use of the function swapcase() to accomplish what you want:

name = "ss A"
print name.swapcase().replace(" ","")
>>> "SSa"
Akshat Mahajan
  • 9,543
  • 4
  • 35
  • 44
0

Don't loop over the string.

str_=raw_input('Enter:')
str_.replace(" ","").swapcase()

the .swapcase() method is not deprecated and exactly what you need.

Sebastian Wozny
  • 16,943
  • 7
  • 52
  • 69