简体   繁体   中英

How to capitalize only first letter of first word in a list

I want to capital only first letter of first word of list and remaining all words to be in lower case in ascending order of the length of word.

z=['The' ,'lines', 'are', 'printed', 'in', 'reverse', 'order']
z.sort(key=len) 
print(" ".join(x.lower() for x in z))

I want the result like this in ascending order of length.

In the are lines order printed reverse :

z=['The' ,'lines', 'are', 'printed', 'in', 'reverse', 'order']
# i tried like this 
z.sort(key=len)
s=[]
for x in z:
     if (x==0):
        s.append(z[0].capitalize())
     else:
        s.append(z[x].lower())

Actual output that I am trying to get:

In the are lines order printed reverse

Your code

for x in z: # ['in', 'The', 'are', 'lines', 'order', 'printed', 'reverse'] if (x==0): s.append(z[0].capitalize()) else: s.append(z[x].lower())

does not work because each x is a word from z - it will never be equal to 0 so all that things added to s are lower case version of your words.


You can use enumerate to get the index of all elements in the list while iterating it.

The first element has to use .title() casing, all others .lower() :

z= ['The' ,'lines', 'are', 'printed', 'in', 'reverse', 'order']
z.sort(key=len) 

# reformat capitalization based in index, any number != 0 is true-thy
z = [ a.lower() if idx else a.capitalize() for idx, a in enumerate(z) ]

print(" ".join(z))

Output:

In the are lines order printed reverse

z = [ a.lower() if idx else a.capitalize() for idx, a in enumerate(z) ] is a list comprehension - as normal loop it would look like:

s = []
for idx, word in enumerate(z):
    if idx:   # any idx > 0 is True-thy
        s.append(word.lower())
    else:
        s.append(word.capitalize())
# print the joined s 

You can learn what python considers True for non-boolean values here: Truth-value-testing

You don't need to use a for loop (or enumerate ), you can just use the capitalize() method if you're sure the type of the first value will always be str .

In other words, call the capitalize() method on the first value of the list and change the value at index 0 to its result.

For example, the following code:

z = ['the' ,'lines', 'are', 'printed', 'in', 'reverse', 'order']
z.sort(key = len)
z[0] = z[0].capitalize()
print(z)

Should output:

['In', 'the', 'are', 'lines', 'order', 'printed', 'reverse']

And if you join it using a space, like this:

z = " ".join(z)

It'll output:

'In the are lines order printed reverse'

WARNING: if you do decide to use join on z , you'll be changing its type from list to str , indexes will still work but z[0] will no longer equal In , it'll be I .


If you want to be extremely thorough and ensure that all other words are lower cased, you can use the following list comprehension instead of the aforementioned technique:

z = ['the' ,'lines', 'are', 'printed', 'in', 'reverse', 'order']
z.sort(key = len)
z = [(x.capitalize() if i == 0 else x) for (i, x) in enumerate(z)]

Expected output:

['In', 'the', 'are', 'lines', 'order', 'printed', 'reverse']

Output when joined using " ".join(z) :

'In the are lines order printed reverse'

Good luck.

Here is the Basic answer output expected by you. you have to call the capitalize() method for the first value in the list and change its value at index 0 to capital.

Here is a basic code to do so

list = ['the' ,'lines', 'are', 'printed', 'in', 'reverse', 'order']
list.sort(key = len)
list[0] = list[0].capitalize()
print(list)

Output will be i the form of an array:

['In', 'the', 'are', 'lines', 'order', 'printed', 'reverse']

to get it in the form of a sentence you can try the code below

list = ['the' ,'lines', 'are', 'printed', 'in', 'reverse', 'order']
list.sort(key = len)
list[0] = list[0].capitalize()
print(" ".join(list))

Output Will be

In the are lines order printed reverse

" ".join(list) Just the list in form of space to convert the output in the form of list to a sentence

I wrote this simple code, I know it's not the most concise but it does the job:

z=['The' ,'lines', 'are', 'printed', 'in', 'reverse', 'order']
s=[]
for x in z:   
    wordup=(x[0].upper() + x[1:len(x)]) 
    s.append(wordup)
print(s)

x is not an integer, we are iterating strings x from the list z

wordup is the concatenation of the first letter of (x) capitalized, and the rest of (x) : this means from index 1 to end of x (len(x))

Using the 'capitalize()' method:

z=['in', 'The', 'are', 'lines', 'order', 'printed', 'reverse']
s=[]
for x in z: 
    s.append(x.capitalize())
print(s)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM