This question already has answers here:
How to change a string into uppercase?
(8 answers)
Closed 9 years ago.
I created a .txt file with random words that I'm importing into my script. I want then to take all the words, print one on each line and convert them all to uppercase. I have the first part done:
a=open("redchief.txt").read().split()
print ' \n'.join(a)
I'm having problems converting the data into capital letters. Here is some of the data:
It looked like a good thing: but wait till I tell you.
Just change your last line from:
print ' \n'.join(a)
to:
print ' \n'.join(a).upper()
you don't have to store the result in a separate variable first, since ' \n'.join(a) gives you a string object whose upper() method you can call.
Related
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 1 year ago.
Is there a way how to remove the property of the print() function in Python that makes end of line after outputting its argument? Or is there a different function to do the output of argument but no the end line?
To illustrate this, lets say I want to output the English alphabet on one line. The following code prints every letter on a new line:
for x in range(26):
print(chr(x+97))
While the second code prints it the way requested, but by concatenation of strings instead:
s=''
for x in range(26):
s+=chr(x+97)
print(s)
print(“Hello”, end=“”)
print(1)
Output: Hello1
Hope this helps :) the default value of end is “\n” and you can change it to anything you want.
This question already has answers here:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 1 year ago.
I am trying to make python take a string, separate the characters before another character eg: "10001001010Q1002000293Q100292Q". I want to separate the string before each Q and have python create either a list or another string. I cannot figure this out for the life of me.
You can do this using the split function, give "Q" as a parameter to the split function then you can slice the list to only get the numbers before Q.
num = "10001001010Q1002000293Q100292Q"
print(num.split("Q")[:-1])
Split() function: https://www.w3schools.com/python/ref_string_split.asp
Slicing: https://www.w3schools.com/python/python_strings_slicing.asp
The syntax is str.split("separator").
str = str.split("Q")
Then output will be ['10001001010', '1002000293', '100292', ''].
If you don't need the last empty element then you can write as:
str = str.split("Q")[:-1]
This question already has answers here:
Removing u in list
(8 answers)
Closed 3 years ago.
I have a list of id's and I am trying the following below:
final = "ids: {}".format(tuple(id_list))
For some reason I am getting the following:
"ids: (u'213231231', u'weqewqqwe')
Could anyone help out on why the u is coming inside my final string. When I am trying the same in another environment, I get the output without the u''. Any specific reason for this?
Actually it is unicode strings in python
for literal value of string you can fist map with str
>>> final = "ids: {}".format(tuple(map(str, id_list)))
>>> final
"ids: ('213231231', 'weqewqqwe')
This question already has answers here:
Converting a list to a string [duplicate]
(8 answers)
Closed 6 years ago.
If I were to get the input of someone and put it into a list. How would I combine this into one big string.
user_input = input()
listed = list(user_input)
I am having trouble with this since the contents are unknown. Is there anyway to make it one big string again(combining all the contents of the list). Is there anything I can import into my code to do this for me
To join a list together, you can use the join method. Simply use it as a method on whatever string you want to have placed between each entry in the list:
>>> ls = ['Hello,','world!']
>>> ' '.join(ls)
'Hello, world!'
This question already has answers here:
Stripping everything but alphanumeric chars from a string in Python
(16 answers)
Closed 9 years ago.
I have a string like 'H897b Site', and I want to test that string to make sure it only contains letters and numbers and no special characters.
So in cases like 'H897b Sit$e' and 'H8(&b Site' I want to be able to flag it.
What I am trying to accomplish is to loop through a column in a table and check for typing errors from the data entry person.
print all(part.isalnum() for part in my_string.split())
You can loops through the string and make checks like
if 'z' >= c >= 'a'
Similarly for numerals etc.