Python String replace() Method Last Updated : 28 Oct, 2024 Comments Improve Suggest changes Like Article Like Report The replace() method replaces all occurrences of a specified substring in a string and returns a new string without modifying the original string.Let’s look at a simple example of replace() method. Python s = "Hello World! Hello Python!" # Replace "Hello" with "Hi" s1 = s.replace("Hello", "Hi") print(s1) OutputHi World! Hi Python! Explanation: Here, "Hello" is replaced by "Hi" throughout the string, resulting in "Hi World! Hi Python!".Note: Since replace() creates a new string, the original string remains unchanged.Table of ContentSyntax of String replace() MethodUsing replace() with Count LimitCase Sensitivity in replace()Syntax of String replace() Methodstring.replace(old, new, count)Parametersold: The substring we want to replace.new: The new substring that we want to replace with old substring.count (optional): Specifies the maximum number of replacements to perform. If omitted, all occurrences are replaced.Return TypeReturns a new string with the specified replacements made. The original string remains unchanged since strings in Python are immutable.Using replace() with Count LimitBy using the optional count parameter, we can limit the number of replacements made. This can be helpful when only a specific number of replacements are desired. Python s = "apple apple apple" # Replace "apple" with "orange" only once s1 = s.replace("apple", "orange", 1) print(s1) Outputorange apple apple Explanation: replace() method only replaces the first instance of "apple" because we specified count=1.Case Sensitivity in replace()The replace() method is case-sensitive, it treats uppercase and lowercase characters as distinct. If we want to replace both cases then we have to use additional logic. Python s = "Hello, World! hello, world!" # Replace only lowercase 'hello' s1 = s.replace("hello", "hi") print(s1) # Replace only uppercase 'Hello' s2 = s.replace("Hello", "Hi") print(s2) OutputHello, World! hi, world! Hi, World! hello, world! Comment More infoAdvertise with us Next Article Python String replace() Method S Striver Follow Improve Article Tags : Misc Python python-string Practice Tags : Miscpython Similar Reads Python string isdecimal() Method In Python, the isdecimal() method is a quick and easy way to check if a string contains only decimal digits. It works by returning True when the string consists of digits from 0 to 9 and False otherwise. This method is especially useful when we want to ensure that user inputs or string data are stri 3 min read Python String isdigit() Method The isdigit() method is a built-in Python function that checks if all characters in a string are digits. This method returns True if each character in the string is a numeric digit (0-9) and False otherwise. Example:Pythona = "12345" print(a.isdigit()) b = "1234a5" print(b.isdigit())OutputTrue False 3 min read Python String isidentifier() Method The isidentifier() method in Python is used to check whether a given string qualifies as a valid identifier according to the Python language rules. Identifiers are names used to identify variables, functions, classes, and other objects. A valid identifier must begin with a letter (A-Z or a-z) or an 3 min read Python String islower() Method The islower() method in Python checks if all characters in a string are lowercase. It returns True if all alphabetic characters are lowercase, otherwise, it returns False, if there is at least one uppercase letter.Let's look at a quick example of using the islower() method.Pythons = "hello" res = s. 2 min read Python String isnumeric() Method The isnumeric() method is a built-in method in Python that belongs to the string class. It is used to determine whether the string consists of numeric characters or not. It returns a Boolean value. If all characters in the string are numeric and it is not empty, it returns âTrueâ If all characters i 3 min read Python String isprintable() Method Python String isprintable() is a built-in method used for string handling. The isprintable() method returns "True" if all characters in the string are printable or the string is empty, Otherwise, It returns "False". This function is used to check if the argument contains any printable characters suc 3 min read Python String isspace() Method isspace() method in Python is used to check if all characters in a string are whitespace characters. This includes spaces (' '), tabs (\t), newlines (\n), and other Unicode-defined whitespace characters. This method is particularly helpful when validating input or processing text to ensure that it c 2 min read Python String istitle() Method The istitle() method in Python is used to check whether a string follows the title case formatting. In a title-cased string, the first letter of each word is capitalized, and all other letters in the word are in lowercase. This method is especially useful when working with formatted text such as tit 3 min read Python String isupper() method isupper() method in Python checks if all the alphabetic characters in a string are uppercase. If the string contains at least one alphabetic character and all of them are uppercase, the method returns True. Otherwise, it returns False. Let's understand this with the help of an example:Pythons = "GEE 3 min read Python String join() Method The join() method in Python is used to concatenate the elements of an iterable (such as a list, tuple, or set) into a single string with a specified delimiter placed between each element.Lets take a simple example to join list of string using join() method.Joining a List of StringsIn below example, 3 min read Like