String Formatting
String formatting provides a more powerful way to embed non-strings within strings. String formatting uses a string's format method to substitute a number of arguments in the string.
Example:
# string formatting
nums = [4, 5, 6]
msg = "Numbers: {0} {1} {2}". format(nums[0], nums[1], nums[2])
print(msg)
Result:>>>
Numbers: 4 5 6
>>>
Each argument of the format function is placed in the string at the corresponding position, which is determined using the curly braces { }.
String formatting can also be done with named arguments.
Example:
a = "{x}, {y}".format(x=5, y=12)
print(a)
Result:>>>
5, 12
more usage
print("{0} - {1}%".format(YourVariable2, round(YouVariable1, 2)))
String Functions
Python contains many useful built-in functions and methods to accomplish common tasks.
join - joins a list of strings with another string as a separator.
replace - replaces one substring in a string with another.
startswith and endswith - determine if there is a substring at the start and end of a string, respectively.
To change the case of a string, you can use lower and upper.
The method split is the opposite of join, turning a string with a certain separator into a list.
Some examples:
print(", ".join(["spam", "eggs", "ham"]))
#prints "spam, eggs, ham"
print("Hello ME".replace("ME", "world"))
#prints "Hello world"
print("This is a sentence.".startswith("This"))
# prints "True"
print("This is a sentence.".endswith("sentence."))
# prints "True"
print("This is a sentence.".upper())
# prints "THIS IS A SENTENCE."
print("AN ALL CAPS SENTENCE".lower())
#prints "an all caps sentence"
print("spam, eggs, ham".split(", "))
#prints "['spam', 'eggs', 'ham']"
eggs.capitalize()
String Concatenation
'1' + '2'
'12'
String Replication
'12'*2
1212
String String - to remove extra spaces and newlines or pass arguments to strip other strings
s=' Extra '
s.srtip()
'Extra'
]
s='********ExtractMe*****'
s.strip(*)
'ExtractMe'
--------------------------------------------------------
Substring Testing
word = 'Hello'
'He' in word
True
Comments
Post a Comment