Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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 ']
 
PREVIOUS NEXT
Tagged: #String #Methods
ADD COMMENT
Topic
Name
8+1 =