Question

In: Computer Science

Python please A string is one of most powerful data types in programming. A string object...

Python please

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

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

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

  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 once it is defined.

The concatenation operator (+) is used to add two or more string objects (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 string object is contained in another string object.

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

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, the default is space.

strObj.strip(strip-character)

Variations: rstrip, lstrip (left and right strip methods to remove 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 new substring

strObj.find(substring) returns the lowest index of the substring if 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 a palindrome, that is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. Note: reverse(string) = string.
  3. Define a string object that will hold “department computer science:fayetteville state university:fayetteville, north carolina”
  4. Split the string object in (3) using the character “:” and assign to a list object
  5. Write a program that capitalize the first letter of every word in 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, space counts in the file storytelling.txt.(The file is on Canvas in the Worksheets folder).

Solutions

Expert Solution

Thanks for the question, Please post only 1 question at a time. I have solved the first two, please post question 3,4 and 5 in a seperate post and I will assist you with that as well.

=================================================================================

# Write a program to reverse a string object
def reverse(s):
    '''
    :param s: input string object
    :return:  reversed string object
    '''
    reversed = ''
    for letter in s:
        reversed = letter + reversed
    return reversed


def main():
    s = input('Enter a string: ')

    reversed_s = reverse(s)
    print('Entered String:',s)
    print('Reversed String:',reversed_s)

main()

==============================================================

# Write a program that will check if a string object is a palindrome, that is a word, phrase, or sequence that reads
def reverse(s):
    '''
    :param s: input string object
    :return:  reversed string object
    '''
    reversed = ''
    for letter in s:
        reversed = letter + reversed
    return reversed


def main():
    s = input('Enter a string or phrase: ')
    words = s.split()
    s_without_spaces = ''.join(words)
    s_without_spaces=s_without_spaces.lower()
    reversed_phrase =reverse(s_without_spaces)
    if reversed_phrase==s_without_spaces:
        print('{} is a palindrome.'.format(s))
    else:
        print('{} is not a palindrome.'.format(s))

main()

===============================================================


Related Solutions

Python please Questions #6 and # 7 please A string is one of most powerful data...
Python please Questions #6 and # 7 please A string is one of most powerful data types in programming. A string object is a sequence of characters and because it is a sequence, it is indexable, using index numbers starting with 0. Similar to a list object, a string object allows for the use of negative index with -1 representing the index of the last character in the sequence. Accessing a string object with an invalid index will result in...
Python please Questions #3, #4 and # 5 please A string is one of most powerful...
Python please Questions #3, #4 and # 5 please A string is one of most powerful data types in programming. A string object is a sequence of characters and because it is a sequence, it is indexable, using index numbers starting with 0. Similar to a list object, a string object allows for the use of negative index with -1 representing the index of the last character in the sequence. Accessing a string object with an invalid index will result...
Write 5 questions about Object Oriented Programming in Python. Each question should have 7 options. Please...
Write 5 questions about Object Oriented Programming in Python. Each question should have 7 options. Please provide 7 answer options for EACH question and the select answer for EACH question
Python has built-in methods to support different manipulations of string data types. Answer what the following...
Python has built-in methods to support different manipulations of string data types. Answer what the following code will print following each Python method. Str = "Tennessee State University" print(len(str)) print(str.lower()) print(str.upper()) substr = “State” print (str.index(substr)) print (str.startswith(“State”)) print (str.split()) print (str.find(substr)) print (str.partition(“State”)) myList = str.split() for words in myList: print (words) newStr = “TN, TX, NY, MA, VA, PA, LA, CA” myStates = newStr.split(‘,’) print (myStates) Show with examples if the above string operations are supported by C++...
Python: How would I modify the class below that takes a string and returns an object...
Python: How would I modify the class below that takes a string and returns an object holding a valid NANP phone number. I am asked to filll in the three methods listed, but underfined, below: __str__(), area_code(), and normalize(). My task is to clean up differently formatted telephone numbers by removing punctuation, such as '(', '-', and the like, and removing and the country code (1) if present. I am asked to start by stripping non-digits, and then see if...
PYTHON PLEASE: Construct a class “Monster” with the following attributes: self.name (a string) self.type (a string,...
PYTHON PLEASE: Construct a class “Monster” with the following attributes: self.name (a string) self.type (a string, default is ‘Normal’) self.current_hp (int, starts out equal to max_hp) self.max_hp (int, is given as input when the class instance is created, default is 20) self.exp (int, starts at 0, is increased by fighting) self.attacks (a dict of all known attacks) self.possible_attacks (a dictionary of all possible attacks) The dictionary of possible_attacks will map the name of an attack (the key) to how many...
The list and dictionary object types are two of the most important and often used types...
The list and dictionary object types are two of the most important and often used types in a Python program. Compare the functionalities of those two objects. What are some ways to insert, update, and remove elements from lists and dictionaries? Why would you choose one data type over another? Provide code examples demonstrating the usages of both data types. Actively participate in this discussion by providing constructive feedback on the criteria, rationales, and examples posted by your peers. Provide...
Java, Python, and C++ are three of the most useful programming languages to learn. Compare the...
Java, Python, and C++ are three of the most useful programming languages to learn. Compare the functionalities of all three programming languages. Why would you choose one language over another? Provide code examples demonstrating their usefulness in a real-world scenario.
Python Programming: Using re library, and a given string recipe1 return the indications after each ingredient...
Python Programming: Using re library, and a given string recipe1 return the indications after each ingredient '@': for example if recipe1 = "@turkey baked @350degrees" print an output like: [@turkey, @350degrees]
This is for C programming. Suppose one string is defined for a sentence with maximum 100...
This is for C programming. Suppose one string is defined for a sentence with maximum 100 characters. Write a method that prints maximum occurring characters in the input string and its frequency. For example, if input string is "this is testt", your code should print 't' : 4. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT