F-String was introduced in Python 3.6, and is now the preferred way of formatting strings. Before Python 3.6 we had to use theformat()method. F-Strings F-string allows you to format selected parts of a string. To specify a string as an f-string, simply put anfin front of the string...
There is no built-in function to reverse a String in Python. The fastest (and easiest?) way is to use a slice that steps backwards,-1. ExampleGet your own Python Server Reverse the string "Hello World": txt ="Hello World"[::-1] ...
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
❮ String Methods ExampleGet your own Python Server UTF-8 encode the string: txt ="My name is Ståle" x = txt.encode() print(x) Run example » Definition and Usage Theencode()method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used...
❮ String Methods ExampleGet your own Python Server Set the tab size to 2 whitespaces: txt ="H\te\tl\tl\to" x = txt.expandtabs(2) print(x) Try it Yourself » Definition and Usage Theexpandtabs()method sets the tab size to the specified number of whitespaces. ...
ExampleGet your own Python Server Join all items in a tuple into a string, using a hash character as separator: myTuple = ("John","Peter","Vicky") x ="#".join(myTuple) print(x) Try it Yourself » Definition and Usage Thejoin()method takes all items in an iterable and joins the...
❮ String Methods ExampleGet your own Python Server Replace the word "bananas": txt ="I like bananas" x = txt.replace("bananas","apples") print(x) Try it Yourself » Definition and Usage Thereplace()method replaces a specified phrase with another specified phrase. ...
ExampleGet your own Python Server Split a string into a list where each word is a list item: txt ="welcome to the jungle" x = txt.split() print(x) Try it Yourself » Definition and Usage Thesplit()method splits a string into a list. ...
❮ String Methods ExampleGet your own Python Server Check if the string starts with "Hello": txt ="Hello, welcome to my world." x = txt.startswith("Hello") print(x) Try it Yourself » Definition and Usage Thestartswith()method returns True if the string starts with the specified va...
ExampleGet your own Python Server Create a mapping table, and use it in thetranslate()method to replace any "S" characters with a "P" character: txt ="Hello Sam!" mytable =str.maketrans("S","P") print(txt.translate(mytable)) ...