- Home
- Course Detail
regularpython@gmail.com
You are now watching:
Python Strings / of String InBuilt Functions
#capitalize() name = "tutorialstution.com" Result = name.capitalize() print(Result) #========================================================================== # count(): name = "tutorialstution.com tutorialstution.com tutorialstution.com" Result = name.count('tutorial') print(Result) #========================================================================== # find():Returns the Highest Index of Substring name = "tutorials tution.com is a website .com" Result = name.find('#') print(Result) #========================================================================== # index():Returns Index of Substring # rindex():Returns last Index of Substring name = "tutorials tution.com is a website .com" Result = name.index('t') print(Result) Result = name.rindex('t') print(Result) #========================================================================== # join():Returns a Concatenated String # returns a string concatenated with the elements of an iterable. numList = ['1', '2', '3', '4'] # seperator = 'world ' print('sai'.join(numList)) #========================================================================== # strip():Removes Trailing Characters # returns a copy of the string with both leading and trailing characters removed # lstrip() # rstrip() string = '-------love------' print(string) print(string.rstrip('-')) #========================================================================== # replace():returns a replaced string # syntax : str.replace(old, new [, count]) wish = 'hello world. How are you? world world' print(wish.replace('world', "sairam", 2)) #========================================================================== # split():Splits String from Left # breaks up a string at the specified separator and returns a list of strings. # Synatx : str.split([separator [, maxsplit]]) # rsplit() grocery = 'Milk, Chicken, Bread, Butter' print(grocery.split(',')) #========================================================================== # title():returns a string with first letter of each word capitalized; a title cased string. text = 'hello world' print(text.title()) # ============================================================================== # upper() # lower() text = 'Hello World' print(text.upper()) print(text.lower()) #center(width, fillchar) name = "tutorialtution.com" Result = name.center(10,'-') print(Result) #========================================================================== #casefold(): firstString = "der Fluß" secondString = "der Fluss" # # # # ß is equivalent to ss if firstString.casefold() == secondString.casefold(): print('The strings are equal.') else: print('The strings are not equal.') #========================================================================== #endswith(): name = "tutorialtution.com" data = ["tutorialtution.com", "tutorialtution.in", "tutoria.com"] for d in data: if d.endswith('.com'): print(d) #========================================================================== #expandtabs():Replaces Tab character With Spaces name = "tutorialtution.com\tis\ta\t\nwebsite" Result = name.expandtabs() print(Result) print(name) #========================================================================== #encode():returns encoded string of given string # unicode string name = 'pyth�n!' Result = name.encode() print(name) print(Result) #========================================================================== #format():formats string into nicer output name = "tutorialstution.com is a website .com" print("{} is a website".format(name)) print("Hello {}, your balance is {}.".format("Adam", 230.2346)) #========================================================================== #ljust():returns left-justified string of given width #returns a left-justified string of a given minimum width. #Syntax : string.ljust(width[, fillchar]) # example string string = 'cat' width = 10 # # # # # print left justified string print(string.ljust(width,' ')) print(string.rjust(width,' ')) #========================================================================== # partition():Returns a Tuple # splits the string at the first occurrence of the argument string and returns a tuple # containing the part the before separator, argument string and the part after the separator #rpartition() string = "Python is fun" # # 'is' separator is found print(string.partition('is ')) grocery = 'Milk, Chicken, Bread, Butter' print(grocery.split(',')) print(grocery.partition(',')) # # 'not' separator is not found print(string.partition('not ')) # string = "Python is fun, isn't it" # splits at first occurence of 'is' print(string.partition('is')) #========================================================================== # startswith():Checks if String Starts with the Specified String text = "Python is easy to learn." # result = text.startswith('is easy') # returns False print(result) result = text.startswith('Python is ') # returns True print(result) # text = "Python programming is easy." # start parameter: 7 # 'programming is easy.' string is searched result = text.startswith('programming is', 3) print(result) # start: 7, end: 18 # 'programming' string is searched result = text.startswith('programming is', 7, 18) print(result) # result = text.startswith('program', 7, 18) print(result) #========================================================================== # zfill():Returns a Copy of The String Padded With Zeros # Syntax : str.zfill(width) text = "program" print(text.zfill(15)) print(text.zfill(20)) print(text.zfill(10)) #========================================================================== # String format_map():Formats the String Using Dictionary # str.format_map(mapping) point = {'x':4,'y':-5} print('{x} {y}'.format(**point))