“Fonctions de chaîne” Réponses codées

Méthodes de cordes


The lower() method simply converts all uppercase letters in a string to 
lowercase letters and returns it. For example:

str1 = "AbCdEfG"
lower_str = str1.lower()
print(lower_str )       # will print "abcdefg" to the terminal

The replace() method takes two arguments, the first is the thing you want to replace, 
and the second is the thing you want to replace it with. These two arguments are 
separated with a comma.
 
For example, if you wanted to replace all full stops in a string with exclamation marks:

str = "Hi. Nice to meet you." 
new_str = str.replace(".", "!")
print(new_str)           # would print "Hi! Nice to meet you!"

The split() method breaks up a string and turns it into a list. 
If you don't pass the split method any arguments, it will split a string at its spaces. 
For example:

str = "What do you think?
"new_str = str.split()
print(new_str)           # would print ['What', 'do', 'you', 'think?']

Or you can pass the split() method a string value to split it up at. 
For example, this code uses the repeated "and" to break up the string into a list.

str = "Oranges and apples and bananas"
fruit_list = str.split("and")
print(fruit_list)        # would print ['Oranges ', ' apples ', ' bananas ']
Gorgeous Goldfinch

Fonctions de chaîne

#def function
--------------------------------------------------------------------------------
def fun(s):
    k=len(s)
    m=' '
    for i in range(0,k):
      
        if s[i].isupper():
            m=m+s[i].lower()
            
        elif s[i].isalpha():
            m=m+s[i].upper()
            
        else:
             if  s[i].isdigit():
                    m=m+ 'll'
             else:
                 if s[i].isspace():
                   m=m + 'e'
                    
                 else:
                     m=m + 'i'
   print(m)
  
fun('rag kI2sbra$n')
--------------------------------------------------------------------------------
#Output:

 '  RAGeKillSBRAiN   '
Gr@Y_orphan_ViLL@in##

Réponses similaires à “Fonctions de chaîne”

Questions similaires à “Fonctions de chaîne”

Plus de réponses similaires à “Fonctions de chaîne” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code