Python please Questions #3, #4 and # 5 please A string is one of most powerful...

50.1K

Verified Solution

Question

Programming

Python please Questions #3, #4 and # 5 please

A string is one of most powerful data types in programming. Astring object is a sequence of characters and because it is asequence, it is indexable, using index numbers starting with 0.Similar to a list object, a string object allows for the use ofnegative index with -1 representing the index of the last characterin the sequence. Accessing a string object with an invalid indexwill result in IndexError exception.

In Python a string literal is defined to be a sequence ofcharacters enclosed in single, double or triple quotes.

To define a string object or variable, we use one of thefollowing:

  1. strObj = string_literal
  2. strObj = str(any object)
  3. strObj = str() or strObj = ‘’, strObj = “”, strObj = ‘’’’’’ ,an empty string

A string object in immutable, meaning, it cannot be changed onceit is defined.

The concatenation operator (+) is used to add two or more stringobjects (strObj1 + strObj2)

The repetition operator (*) is used to repeat the string object(n*strObj or strObj*n, where n is an integer)

The in and not in operators are used to test if one stringobject is contained in another string object.

Slice a string object using strObj[start:end], start is theindex of the starting character and end-1 is the index lastcharacter.

The split method creates a list of the split string object.

lstObj = strObj.split(split-character)

space is the default split-character.

The strip method removes the leading and trailing character, thedefault is space.

strObj.strip(strip-character)

Variations: rstrip, lstrip (left and right strip methods toremove leading and trailing character, respectively)

Other commonly used string methods are

strObj.lower() changes all characters to lowercase

strObj.upper() changes all characters to uppercase

strObj.islower() tests if all characters are lowercase

strObj.isupper() tests if all characters are uppercase

strObj.replace(old, new) replaces old substring with newsubstring

strObj.find(substring) returns the lowest index of the substringif it is found in strObj

Activity

  1. Write a program to reverse a string object
  2. Write a program that will check if a string object is apalindrome, that is a word, phrase, or sequence that reads the samebackward as forward, e.g., madam or nurses run.Note: reverse(string) = string.
  3. Define a string object that will hold “department computerscience:fayetteville state university:fayetteville, northcarolina”
  4. Split the string object in (3) using the character “:” andassign to a list object
  5. Write a program that capitalize the first letter of every wordin string object in (3)
  6. Write a program that insert the string objects “of” and “28301”to the new string object in (5)
  7. Write a program that will display the word, character, spacecounts in the file storytelling.txt.(The file is on Canvas in theWorksheets folder).

Answer & Explanation Solved by verified expert
4.3 Ratings (526 Votes)
1 Write a program to reverse a string object Ans strObj Sangeetha1 printstrObj 2 Write a program that will check if a string object is a palindrome that is a word phrase or sequence that reads the same backward as forward eg madam or nurses run Note reversestring string Ans def isPalindromestrObj return strObj strObj1 strObj bananab ans isPalindromestrObj if ans printIt is Palindrome else printIt is not a    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students