0

I want to know how can I get the string from terminal or command prompt and make lower case that string and display it on terminal or command prompt
I used this code but it's not working

// in main.py cmd or terminal

import sys
tdf = sys.argv[1]
print(tdf)

and in terminal

// in cmd or terminal

PS E:\Python\Project\At> python .\main.py "Hi"

when I used this in my terminal I got this error

File "C:\Users\miladm\AppData\Local\Programs\Python\Python39\lib\encodings\cp1256.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\u06cc' in position 13: character maps to

  • I'm having a hard time reproducing this error, could you give some more info about your python version, operating system, and so on? – AJ Biffl Nov 09 '21 at 06:25

2 Answers2

0

Code:

import sys

print ("Data from terminal :",sys.argv)

while running the script use:

D:\python>python cmdline.py "hello"

Output:

Data from terminal : ['cmdline.py', 'hello']
0

run with python filename.py "arguments"

instead of

tdf = sys.argv[1]

Use,

tdf = sys.argv[-1]

because it will get the last argument on the command line. If no arguments are passed, it will be the script name itself, as sys.argv[0] is the name of the running program. in your case if there is no arguments passed it will create an error

import sys
tdf = sys.argv[-1]
print(tdf)
SANGEETH SUBRAMONIAM
  • 1,098
  • 1
  • 9
  • 10