Simple question about the title () command

2

If I put for example the name:

  

roberto fair da silva

using title it looks like this:

  

Roberto Justo Da Silva

How do I make the command title do not modify the word 'da' to look like this:

  

Roberto Justo da Silva

Obviously I used a simple example but I would like a way to change if I had the names in a txt file and with multiple last names where 'da' appears in different places.     

asked by anonymous 07.10.2018 / 22:36

3 answers

1

A good way to do this is by separating your input string with split by space, then save it to array de string . Now you only have to compare the elements of the array, when the element is equal to da ou de does not apply the title to it i=0 string_nome=input("Digite o nome:") nome_dividido=string_nome.split(' ') while(len(nome_dividido)>i): print(nome_dividido[i]) if(nome_dividido[i] == 'da' or nome_dividido[i] == 'de'): nome_dividido[i]=nome_dividido[i] else: nome_dividido[i]=str.title(str(nome_dividido[i])) i=i+1 print(nome_dividido) The only problem is that you would need to turn this array back into string.

    
08.10.2018 / 19:30
3

Not a command is a method and it can only be applied to data of type string . Documentation . It is very basic and does not handle exceptions, which in fact decreases its usefulness. So you have to create something manual that sweeps all% of% and modify the letter box according to the rules you set. That is, you have to create an parse algorithm. If you do not call for efficiency you can use this method but treat those words in a slightly different way in another step. has been shown in PHP . It was there to help more because there was an attempt, here the question is more conceptual, so the answer is more conceptual.

    
07.10.2018 / 22:45
2

Using the idea of the friend above I did a function to give you an idea to maybe solve your problem.

def newTitle(name):
    name=name.split(' ')
    exceptions=['da','de']
    titledName=''
    for i in name:
        if i in exceptions:
            titledName+=i
        else:
            titledName+=i.title()
        titledName+=' '
    return(titledName[:len(titledName)-1])
    
09.10.2018 / 20:31