Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

String Methods


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 ']
Comment

string functions

#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   '
Comment

PREVIOUS NEXT
Code Example
Python :: where python packages are installed 
Python :: Getting the data type 
Python :: if it is square python 
Python :: python dictionary if not found 
Python :: how to inherit a class in python 
Python :: scaling 
Python :: fraction in python 
Python :: set empty dictionary key python 
Python :: IndexError: list assignment index out of range 
Python :: change value of column in pandas 
Python :: why is python so popular 
Python :: df.rename(index=str, columns={"A": "a", "C": "c"}) what does index=str means 
Python :: find the range in python 
Python :: any and all in python3 
Python :: python string format_map 
Python :: python catching exceptions 
Python :: Math Module floor() Function in python 
Python :: how to make a calcukatir un python 
Python :: How to get the Tkinter Label text 
Python :: series change index pandas 
Python :: python loop over list 
Python :: pythagore 
Python :: sphinx themes 
Python :: first non repeating charcter in string ython 
Python :: how to make window pygame 
Python :: how to print the 3erd character of an input in python 
Python :: prime numbers 1 to input 
Python :: discord api python putting ids in a list 
Python :: function TBone() if 2=2 then print("Sup") end 
Python :: directory corrente python 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =