Teachnique
      CourseRoadmaps
      Login

      OverviewCommentsUser InputNumbersBooleansHistoryHello World ProgramEnvironment SetupSyntaxVariablesData TypesType CastingUnicode SystemLiteralsOperators

      Control FlowBreak StatementContinue StatementPass StatementNested LoopsDecision MakingIf StatementIf-else StatementNested IF StatementMatch-Case StatementLoopsFor LoopsFor-else LoopsWhile Loops

      FunctionsBuilt-in FunctionsDefault ArgumentsKeyword ArgumentsKeyword-Only ArgumentsPositional ArgumentsPositional-Only ArgumentsArbitrary ArgumentsVariable ScopeFunction AnnotationsModules

      StringSlicing StringsModify StringsString ConcatenationString FormattingEscape CharactersString MethodsString Exercises

      ListsList ExercisesAccess List ItemsChange List ItemsAdd List ItemsRemove List ItemsLoop ListsList ComprehensionSort ListsCopy ListsJoin ListsList Methods

      TuplesAccess Tuple ItemsUpdate TuplesUnpack Tuple ItemsLoop TuplesJoin TuplesTuple MethodsTuple Exercises

      SetsAccess Set ItemsAdd Set ItemsRemove Set ItemsLoop SetsJoin SetsCopy SetsSet OperatorsSet MethodsSet Exercises

      DictionariesDictionary ExercisesAccess Dictionary ItemsChange Dictionary ItemsAdd Dictionary ItemsRemove Dictionary ItemsDictionary View ObjectsLoop DictionariesCopy DictionariesNested DictionariesDictionary Methods

      ArraysAccess Array ItemsAdd Array ItemsRemove Array ItemsLoop ArraysCopy ArraysReverse ArraysSort ArraysJoin ArraysArray MethodsArray Exercises

      File HandlingWrite to FileRead FilesRenaming and Deleting FilesDirectoriesFile Methods

      OOP ConceptsDynamic BindingDynamic TypingAbstractionObject and ClassesEncapsulationInterfacesPackagesInner ClassesAnonymous Class and ObjectsSingleton ClassWrapper ClassesEnumsReflectionClass AttributesClass MethodsStatic MethodsConstructorsAccess ModifiersInheritancePolymorphismMethod OverridingMethod Overloading

      Feedback

      Submit request if you have any questions.

      Course
      String Exercises

      Python Tutorial

      This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.

      String Exercises

      Example 1

      Python program to find number of vowels in a given string.
      mystr = "All animals are equal. Some are more equal"
      vowels = "aeiou"
      count=0
      for x in mystr:
      if x.lower() in vowels: count+=1
      print ("Number of Vowels:", count)
      It will produce the following output
      Number of Vowels: 18

      Example 2

      Python program to convert a string with binary digits to integer.
      mystr = '10101'
      
      def strtoint(mystr):
      for x in mystr:
      if x not in '01': return "Error. String with non-binary characters"
      num = int(mystr, 2)
      return num
      print ("binary:{} integer: {}".format(mystr,strtoint(mystr)))
      It will produce the following output
      binary:10101 integer: 21
      Change mystr to '10, 101'
      binary:10,101 integer: Error. String with non-binary characters

      Example 3

      Python program to drop all digits from a string.
      digits = [str(x) for x in range(10)]
      mystr = 'He12llo, Py00th55on!'
      chars = []
      for x in mystr:
      if x not in digits:
      chars.append(x)
      newstr = ''.join(chars)
      print (newstr)
      It will produce the following output
      Hello, Python!

      Exercise Programs

      • Python program to sort the characters in a string
      • Python program to remove duplicate characters from a string
      • Python program to list unique characters with their count in a string
      • Python program to find number of words in a string
      • Python program to remove all non-alphabetic characters from a string

      Practice with Online Editor

      Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
      Remember to save code(Ctrl + S Or Command + S) before run it.