Convert string to all lower case without built-in functions









up vote
1
down vote

favorite












#variable defination#
lower="abcdefghijklmnopqrstuvwxyz"
upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
valid=True
x=0
g=0


string=input("enter a string:")

#data validation#
for char in string:
if char in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
valid=True
else:
valid=False

#finding the character#
if valid:
for char in string:
g+=1
for ele in upper:
if char!=ele:
x+=1


print(lower[x]+string[g::])


**I can't get it to work, it keeps iterating through the entire string without the condition ever being met. **










share|improve this question



























    up vote
    1
    down vote

    favorite












    #variable defination#
    lower="abcdefghijklmnopqrstuvwxyz"
    upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    valid=True
    x=0
    g=0


    string=input("enter a string:")

    #data validation#
    for char in string:
    if char in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
    valid=True
    else:
    valid=False

    #finding the character#
    if valid:
    for char in string:
    g+=1
    for ele in upper:
    if char!=ele:
    x+=1


    print(lower[x]+string[g::])


    **I can't get it to work, it keeps iterating through the entire string without the condition ever being met. **










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      #variable defination#
      lower="abcdefghijklmnopqrstuvwxyz"
      upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      valid=True
      x=0
      g=0


      string=input("enter a string:")

      #data validation#
      for char in string:
      if char in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
      valid=True
      else:
      valid=False

      #finding the character#
      if valid:
      for char in string:
      g+=1
      for ele in upper:
      if char!=ele:
      x+=1


      print(lower[x]+string[g::])


      **I can't get it to work, it keeps iterating through the entire string without the condition ever being met. **










      share|improve this question















      #variable defination#
      lower="abcdefghijklmnopqrstuvwxyz"
      upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      valid=True
      x=0
      g=0


      string=input("enter a string:")

      #data validation#
      for char in string:
      if char in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
      valid=True
      else:
      valid=False

      #finding the character#
      if valid:
      for char in string:
      g+=1
      for ele in upper:
      if char!=ele:
      x+=1


      print(lower[x]+string[g::])


      **I can't get it to work, it keeps iterating through the entire string without the condition ever being met. **







      python string






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 4:55









      jpp

      85.5k194898




      85.5k194898










      asked Nov 10 at 3:36









      Sree The Tree

      61




      61






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          1
          down vote













          I tried to minimize changes from your original code (But remember, it's obvious that other solutions are much better.)



          #variable defination#
          lower="abcdefghijklmnopqrstuvwxyz"
          upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

          string=input("enter a string:")

          #data validation#
          valid = True
          for char in string:
          if char not in lower + upper:
          valid=False

          #finding the character#
          if valid:
          result = ""
          for char in string:
          if char in lower:
          result += char
          else:
          # uppercase character
          for i in range(len(upper)):
          if char == upper[i]:
          result += lower[i]
          break

          print(result)





          share|improve this answer





























            up vote
            0
            down vote













            Since you are allowed to use lowercase and uppercase character inputs, you can create a dictionary mapping between them and use str.join with a list comprehension:



            from string import ascii_lowercase, ascii_uppercase

            d = dict(zip(ascii_uppercase, ascii_lowercase))

            string = input("enter a string:")

            res = ''.join([d.get(i, i) for i in string])


            It's not clear whether this satisfies your "no in-built function" requirement.






            share|improve this answer



























              up vote
              0
              down vote













              You can create mapping(dictionary) between uppercase and lowercase alphabets such that upper_to_lower[uppercase] should give you lowercase. You may refer below implementation as reference though.



              import string

              def to_lower_case(word):
              # construct auxilliary dictionary to avoid ord and chr built-in methods
              upper_to_lower =
              for index in range(len(string.ascii_uppercase)): # this is 26 we can use it as constant though
              upper_to_lower[string.ascii_uppercase[index]] = string.ascii_lowercase[index]

              result = ''
              for alphabet in word:
              if alphabet in string.ascii_uppercase:
              result += upper_to_lower[alphabet]
              else:
              result += alphabet

              return result

              # sample input
              In [5]: to_lower_case('ANJSNJN48982984aadkaka')
              Out[5]: 'anjsnjn48982984aadkaka'





              share|improve this answer



























                up vote
                0
                down vote













                You can also write an index method to get the index of an uppercase character in the upper string:



                lower = "abcdefghijklmnopqrstuvwxyz"
                upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

                def index(char):
                i = 0
                for c in upper:
                if char == c: return i
                i += 1


                Then you can convert your string to lowercase like this:



                #data validation#
                # ...

                #finding the character#
                s = ''
                if valid:
                for char in string:
                if char in lower: s += char
                else: s += lower[index(char)]

                print(s)





                share|improve this answer




















                  Your Answer






                  StackExchange.ifUsing("editor", function ()
                  StackExchange.using("externalEditor", function ()
                  StackExchange.using("snippets", function ()
                  StackExchange.snippets.init();
                  );
                  );
                  , "code-snippets");

                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "1"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235797%2fconvert-string-to-all-lower-case-without-built-in-functions%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  1
                  down vote













                  I tried to minimize changes from your original code (But remember, it's obvious that other solutions are much better.)



                  #variable defination#
                  lower="abcdefghijklmnopqrstuvwxyz"
                  upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

                  string=input("enter a string:")

                  #data validation#
                  valid = True
                  for char in string:
                  if char not in lower + upper:
                  valid=False

                  #finding the character#
                  if valid:
                  result = ""
                  for char in string:
                  if char in lower:
                  result += char
                  else:
                  # uppercase character
                  for i in range(len(upper)):
                  if char == upper[i]:
                  result += lower[i]
                  break

                  print(result)





                  share|improve this answer


























                    up vote
                    1
                    down vote













                    I tried to minimize changes from your original code (But remember, it's obvious that other solutions are much better.)



                    #variable defination#
                    lower="abcdefghijklmnopqrstuvwxyz"
                    upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

                    string=input("enter a string:")

                    #data validation#
                    valid = True
                    for char in string:
                    if char not in lower + upper:
                    valid=False

                    #finding the character#
                    if valid:
                    result = ""
                    for char in string:
                    if char in lower:
                    result += char
                    else:
                    # uppercase character
                    for i in range(len(upper)):
                    if char == upper[i]:
                    result += lower[i]
                    break

                    print(result)





                    share|improve this answer
























                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      I tried to minimize changes from your original code (But remember, it's obvious that other solutions are much better.)



                      #variable defination#
                      lower="abcdefghijklmnopqrstuvwxyz"
                      upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

                      string=input("enter a string:")

                      #data validation#
                      valid = True
                      for char in string:
                      if char not in lower + upper:
                      valid=False

                      #finding the character#
                      if valid:
                      result = ""
                      for char in string:
                      if char in lower:
                      result += char
                      else:
                      # uppercase character
                      for i in range(len(upper)):
                      if char == upper[i]:
                      result += lower[i]
                      break

                      print(result)





                      share|improve this answer














                      I tried to minimize changes from your original code (But remember, it's obvious that other solutions are much better.)



                      #variable defination#
                      lower="abcdefghijklmnopqrstuvwxyz"
                      upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

                      string=input("enter a string:")

                      #data validation#
                      valid = True
                      for char in string:
                      if char not in lower + upper:
                      valid=False

                      #finding the character#
                      if valid:
                      result = ""
                      for char in string:
                      if char in lower:
                      result += char
                      else:
                      # uppercase character
                      for i in range(len(upper)):
                      if char == upper[i]:
                      result += lower[i]
                      break

                      print(result)






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 10 at 4:44

























                      answered Nov 10 at 4:38









                      JH Jang

                      263




                      263






















                          up vote
                          0
                          down vote













                          Since you are allowed to use lowercase and uppercase character inputs, you can create a dictionary mapping between them and use str.join with a list comprehension:



                          from string import ascii_lowercase, ascii_uppercase

                          d = dict(zip(ascii_uppercase, ascii_lowercase))

                          string = input("enter a string:")

                          res = ''.join([d.get(i, i) for i in string])


                          It's not clear whether this satisfies your "no in-built function" requirement.






                          share|improve this answer
























                            up vote
                            0
                            down vote













                            Since you are allowed to use lowercase and uppercase character inputs, you can create a dictionary mapping between them and use str.join with a list comprehension:



                            from string import ascii_lowercase, ascii_uppercase

                            d = dict(zip(ascii_uppercase, ascii_lowercase))

                            string = input("enter a string:")

                            res = ''.join([d.get(i, i) for i in string])


                            It's not clear whether this satisfies your "no in-built function" requirement.






                            share|improve this answer






















                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              Since you are allowed to use lowercase and uppercase character inputs, you can create a dictionary mapping between them and use str.join with a list comprehension:



                              from string import ascii_lowercase, ascii_uppercase

                              d = dict(zip(ascii_uppercase, ascii_lowercase))

                              string = input("enter a string:")

                              res = ''.join([d.get(i, i) for i in string])


                              It's not clear whether this satisfies your "no in-built function" requirement.






                              share|improve this answer












                              Since you are allowed to use lowercase and uppercase character inputs, you can create a dictionary mapping between them and use str.join with a list comprehension:



                              from string import ascii_lowercase, ascii_uppercase

                              d = dict(zip(ascii_uppercase, ascii_lowercase))

                              string = input("enter a string:")

                              res = ''.join([d.get(i, i) for i in string])


                              It's not clear whether this satisfies your "no in-built function" requirement.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 10 at 4:18









                              jpp

                              85.5k194898




                              85.5k194898




















                                  up vote
                                  0
                                  down vote













                                  You can create mapping(dictionary) between uppercase and lowercase alphabets such that upper_to_lower[uppercase] should give you lowercase. You may refer below implementation as reference though.



                                  import string

                                  def to_lower_case(word):
                                  # construct auxilliary dictionary to avoid ord and chr built-in methods
                                  upper_to_lower =
                                  for index in range(len(string.ascii_uppercase)): # this is 26 we can use it as constant though
                                  upper_to_lower[string.ascii_uppercase[index]] = string.ascii_lowercase[index]

                                  result = ''
                                  for alphabet in word:
                                  if alphabet in string.ascii_uppercase:
                                  result += upper_to_lower[alphabet]
                                  else:
                                  result += alphabet

                                  return result

                                  # sample input
                                  In [5]: to_lower_case('ANJSNJN48982984aadkaka')
                                  Out[5]: 'anjsnjn48982984aadkaka'





                                  share|improve this answer
























                                    up vote
                                    0
                                    down vote













                                    You can create mapping(dictionary) between uppercase and lowercase alphabets such that upper_to_lower[uppercase] should give you lowercase. You may refer below implementation as reference though.



                                    import string

                                    def to_lower_case(word):
                                    # construct auxilliary dictionary to avoid ord and chr built-in methods
                                    upper_to_lower =
                                    for index in range(len(string.ascii_uppercase)): # this is 26 we can use it as constant though
                                    upper_to_lower[string.ascii_uppercase[index]] = string.ascii_lowercase[index]

                                    result = ''
                                    for alphabet in word:
                                    if alphabet in string.ascii_uppercase:
                                    result += upper_to_lower[alphabet]
                                    else:
                                    result += alphabet

                                    return result

                                    # sample input
                                    In [5]: to_lower_case('ANJSNJN48982984aadkaka')
                                    Out[5]: 'anjsnjn48982984aadkaka'





                                    share|improve this answer






















                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      You can create mapping(dictionary) between uppercase and lowercase alphabets such that upper_to_lower[uppercase] should give you lowercase. You may refer below implementation as reference though.



                                      import string

                                      def to_lower_case(word):
                                      # construct auxilliary dictionary to avoid ord and chr built-in methods
                                      upper_to_lower =
                                      for index in range(len(string.ascii_uppercase)): # this is 26 we can use it as constant though
                                      upper_to_lower[string.ascii_uppercase[index]] = string.ascii_lowercase[index]

                                      result = ''
                                      for alphabet in word:
                                      if alphabet in string.ascii_uppercase:
                                      result += upper_to_lower[alphabet]
                                      else:
                                      result += alphabet

                                      return result

                                      # sample input
                                      In [5]: to_lower_case('ANJSNJN48982984aadkaka')
                                      Out[5]: 'anjsnjn48982984aadkaka'





                                      share|improve this answer












                                      You can create mapping(dictionary) between uppercase and lowercase alphabets such that upper_to_lower[uppercase] should give you lowercase. You may refer below implementation as reference though.



                                      import string

                                      def to_lower_case(word):
                                      # construct auxilliary dictionary to avoid ord and chr built-in methods
                                      upper_to_lower =
                                      for index in range(len(string.ascii_uppercase)): # this is 26 we can use it as constant though
                                      upper_to_lower[string.ascii_uppercase[index]] = string.ascii_lowercase[index]

                                      result = ''
                                      for alphabet in word:
                                      if alphabet in string.ascii_uppercase:
                                      result += upper_to_lower[alphabet]
                                      else:
                                      result += alphabet

                                      return result

                                      # sample input
                                      In [5]: to_lower_case('ANJSNJN48982984aadkaka')
                                      Out[5]: 'anjsnjn48982984aadkaka'






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 10 at 4:29









                                      Yaman Jain

                                      803812




                                      803812




















                                          up vote
                                          0
                                          down vote













                                          You can also write an index method to get the index of an uppercase character in the upper string:



                                          lower = "abcdefghijklmnopqrstuvwxyz"
                                          upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

                                          def index(char):
                                          i = 0
                                          for c in upper:
                                          if char == c: return i
                                          i += 1


                                          Then you can convert your string to lowercase like this:



                                          #data validation#
                                          # ...

                                          #finding the character#
                                          s = ''
                                          if valid:
                                          for char in string:
                                          if char in lower: s += char
                                          else: s += lower[index(char)]

                                          print(s)





                                          share|improve this answer
























                                            up vote
                                            0
                                            down vote













                                            You can also write an index method to get the index of an uppercase character in the upper string:



                                            lower = "abcdefghijklmnopqrstuvwxyz"
                                            upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

                                            def index(char):
                                            i = 0
                                            for c in upper:
                                            if char == c: return i
                                            i += 1


                                            Then you can convert your string to lowercase like this:



                                            #data validation#
                                            # ...

                                            #finding the character#
                                            s = ''
                                            if valid:
                                            for char in string:
                                            if char in lower: s += char
                                            else: s += lower[index(char)]

                                            print(s)





                                            share|improve this answer






















                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              You can also write an index method to get the index of an uppercase character in the upper string:



                                              lower = "abcdefghijklmnopqrstuvwxyz"
                                              upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

                                              def index(char):
                                              i = 0
                                              for c in upper:
                                              if char == c: return i
                                              i += 1


                                              Then you can convert your string to lowercase like this:



                                              #data validation#
                                              # ...

                                              #finding the character#
                                              s = ''
                                              if valid:
                                              for char in string:
                                              if char in lower: s += char
                                              else: s += lower[index(char)]

                                              print(s)





                                              share|improve this answer












                                              You can also write an index method to get the index of an uppercase character in the upper string:



                                              lower = "abcdefghijklmnopqrstuvwxyz"
                                              upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

                                              def index(char):
                                              i = 0
                                              for c in upper:
                                              if char == c: return i
                                              i += 1


                                              Then you can convert your string to lowercase like this:



                                              #data validation#
                                              # ...

                                              #finding the character#
                                              s = ''
                                              if valid:
                                              for char in string:
                                              if char in lower: s += char
                                              else: s += lower[index(char)]

                                              print(s)






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 10 at 5:25









                                              slider

                                              7,3401129




                                              7,3401129



























                                                  draft saved

                                                  draft discarded
















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  To learn more, see our tips on writing great answers.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235797%2fconvert-string-to-all-lower-case-without-built-in-functions%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                                                  Syphilis

                                                  Darth Vader #20