Question

In: Computer Science

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++ library OR the Java String class.

Solutions

Expert Solution

str = "Tennessee State University"

print(len(str)) ###prints length of string ie.26
print(str.lower())##prints all letters in lower case ie: tennessee state university
print(str.upper())#prints TENNESSEE STATE UNIVERSITY
substr ="State"
print (str.index(substr))# prints 10
print (str.startswith("State")) #prints false
print (str.split())# prints ['Tennessee', 'State', 'University']
print (str.find(substr)) #prints 10
print (str.partition("State")) # prints ('Tennessee ', 'State', ' University')
myList = str.split()
for words in myList:
print (words) #prints Tennessee State University
newStr = "TN, TX, NY, MA, VA, PA, LA, CA"
myStates = newStr.split(',')
print (myStates) #prints ['TN', ' TX', ' NY', ' MA', ' VA', ' PA', ' LA', ' CA']

##############output:##########

26

tennessee state university                                                                                                      

TENNESSEE STATE UNIVERSITY                                                                                                      

10                                                                                                                              

False                                                                                                                           

['Tennessee', 'State', 'University']                                                                                            

10                                                                                                                              

('Tennessee ', 'State', ' University')                                                                                          

Tennessee                                                                                                                       

State                                                                                                                           

University                                                                                                                      

['TN', ' TX', ' NY', ' MA', ' VA', ' PA', ' LA', ' CA']

################ java code###########


public class Mystring {

   public static void main(String[] args) {
       String str = "Tennessee State University";
       System.out.println(str.length());
       System.out.println(str.toLowerCase());
       System.out.println(str.toUpperCase());
       String substr="State";
       System.out.println(str.indexOf(substr));
       System.out.println(str.startsWith(substr));
       String a[]=str.split(" ");
       System.out.print("[");
       for(int i=0;i<a.length;i++)
           System.out.print(a[i]+", ");
       System.out.println("]");
       System.out.println(str.indexOf(substr));
       String b[]=str.split("State");
       System.out.print("(");
       for(int i=0;i<b.length;i++)
           System.out.print(b[i]+", ");
       System.out.println(")");
       a=str.split(" ");
       for(int i=0;i<a.length;i++)
           System.out.println(a[i]);
       String newStr="TN, TX, NY, MA, VA, PA, LA, CA";
       a=str.split(" ");
       System.out.print("[");
       for(int i=0;i<a.length;i++)
           System.out.print(a[i]+", ");
       System.out.println("]");
   }

}


Related Solutions

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...
Discuss the different methods to account for cash discount. Support you answer with examples. Answer: During...
Discuss the different methods to account for cash discount. Support you answer with examples. Answer: During April, the following changes in the single inventory product took place           April   1     Balance                        1,400 units @ $24                       8     Purchased                      900 units @ $36                     12     Purchased                      700 units @ $30                     24     Purchased                      400 units @ $50                     10     Sold                              1,500 units @ $40                     26     Sold                              1,700 units @ $44 Calculate the COGS after each sales transaction and the ending...
Discuss the different methods to account for cash discount. Support you answer with examples
Discuss the different methods to account for cash discount. Support you answer with examples
1. What are the main data types in Python? Which data types are sequences? 2. What...
1. What are the main data types in Python? Which data types are sequences? 2. What are some general guidelines for naming a ‘variable’ in Python? Give at least three examples of variable names that follow these guidelines 3. Provide an example of the Python syntax to create a variable to hold the age of a building (for example,40 years old). a. Provide the Python syntax to test whether the variable created above is more than 35, and print the...
Q2. Discuss the different methods to account for cash discount. Support you answer with examples.
Q2. Discuss the different methods to account for cash discount. Support you answer with examples.
implement the following logic in Python, use appropriate data types. Data types are represented as either...
implement the following logic in Python, use appropriate data types. Data types are represented as either numeric (num) or string. string name string address num item num quantity num price num SIZE = 6 num VALID_ITEM [SIZE] = 106, 108, 307, 405, 457, 688 num VALID_ITEM_PRICE [SIZE] = 0.59, 0.99, 4.50, 15.99, 17.50, 39.00 num sub string foundIt = “N” string MSG_YES = “Item available” string MSG_NO = “Item not found” get name, address, item, quantity sub = 0 while...
In the following Java class, what would the following createFromString(String string) and saveToString() methods return? /**...
In the following Java class, what would the following createFromString(String string) and saveToString() methods return? /** * This class represents a DVD player to be rented. * * @author Franklin University * @version 2.0 */ public class DVDPlayer extends AbstractItem { /** * Constructor for objects of class DVDPlayer. */ public DVDPlayer() { // No code needed } /** * Creates a DVDPlayer from a string in the format * id:desc:weeklyRate:rented * @param string The string * @return the new...
Q2. Discuss the different methods to account for cash discount. Support you answer with examples. (1...
Q2. Discuss the different methods to account for cash discount. Support you answer with examples. (1 mark).
what are the different types of sampling methods in a research and when are they used...
what are the different types of sampling methods in a research and when are they used ?
Explain what is data and list the different types of data? List and explain the different...
Explain what is data and list the different types of data? List and explain the different methods to collect data.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT