Given the string object value online education what is the output of value substring 3

Python has a built-in string class named "str" with many handy features [there is an older module named "string" which you should not use]. String literals can be enclosed by either double or single quotes, although single quotes are more commonly used. Backslash escapes work the usual way within both single and double quoted literals -- e.g. \n \' \". A double quoted string literal can contain single quotes without any fuss [e.g. "I didn't do it"] and likewise single quoted string can contain double quotes. A string literal can span multiple lines, but there must be a backslash \ at the end of each line to escape the newline. String literals inside triple quotes, """ or ''', can span multiple lines of text.

Python strings are "immutable" which means they cannot be changed after they are created [Java strings also use this immutable style]. Since strings can't be changed, we construct *new* strings as we go to represent computed values. So for example the expression ['hello' + 'there'] takes in the 2 strings 'hello' and 'there' and builds a new string 'hellothere'.

Characters in a string can be accessed using the standard [ ] syntax, and like Java and C++, Python uses zero-based indexing, so if s is 'hello' s[1] is 'e'. If the index is out of bounds for the string, Python raises an error. The Python style [unlike Perl] is to halt if it can't tell what to do, rather than just make up a default value. The handy "slice" syntax [below] also works to extract any substring from a string. The len[string] function returns the length of a string. The [ ] syntax and the len[] function actually work on any sequence type -- strings, lists, etc.. Python tries to make its operations work consistently across different types. Python newbie gotcha: don't use "len" as a variable name to avoid blocking out the len[] function. The '+' operator can concatenate two strings. Notice in the code below that variables are not pre-declared -- just assign to them and go.

  s = 'hi'
  print s[1]          ## i
  print len[s]        ## 2
  print s + ' there'  ## hi there

Unlike Java, the '+' does not automatically convert numbers or other types to string form. The str[] function converts values to a string form so they can be combined with other strings.

  pi = 3.14
  ##text = 'The value of pi is ' + pi      ## NO, does not work
  text = 'The value of pi is '  + str[pi]  ## yes

For numbers, the standard operators, +, /, * work in the usual way. There is no ++ operator, but +=, -=, etc. work. If you want integer division, use 2 slashes -- e.g. 6 // 5 is 1

The "print" function normally prints out one or more python items followed by a newline. A "raw" string literal is prefixed by an 'r' and passes all the chars through without special treatment of backslashes, so r'x\nx' evaluates to the length-4 string 'x\nx'. "print" can take several arguments to change how it prints things out [see python.org print function definition] like setting "end" to "" to no longer print a newline after it finishes printing out all of the items.

  raw = r'this\t\n and that'

  # this\t\n and that
  print[raw]

  multi = """It was the best of times.
  It was the worst of times."""

  # It was the best of times.
  #   It was the worst of times.
print[multi]

String Methods

Here are some of the most common string methods. A method is like a function, but it runs "on" an object. If the variable s is a string, then the code s.lower[] runs the lower[] method on that string object and returns the result [this idea of a method running on an object is one of the basic ideas that make up Object Oriented Programming, OOP]. Here are some of the most common string methods:

  • s.lower[], s.upper[] -- returns the lowercase or uppercase version of the string
  • s.strip[] -- returns a string with whitespace removed from the start and end
  • s.isalpha[]/s.isdigit[]/s.isspace[]... -- tests if all the string chars are in the various character classes
  • s.startswith['other'], s.endswith['other'] -- tests if the string starts or ends with the given other string
  • s.find['other'] -- searches for the given other string [not a regular expression] within s, and returns the first index where it begins or -1 if not found
  • s.replace['old', 'new'] -- returns a string where all occurrences of 'old' have been replaced by 'new'
  • s.split['delim'] -- returns a list of substrings separated by the given delimiter. The delimiter is not a regular expression, it's just text. 'aaa,bbb,ccc'.split[','] -> ['aaa', 'bbb', 'ccc']. As a convenient special case s.split[] [with no arguments] splits on all whitespace chars.
  • s.join[list] -- opposite of split[], joins the elements in the given list together using the string as the delimiter. e.g. '---'.join[['aaa', 'bbb', 'ccc']] -> aaa---bbb---ccc

A google search for "python str" should lead you to the official python.org string methods which lists all the str methods.

Python does not have a separate character type. Instead an expression like s[8] returns a string-length-1 containing the character. With that string-length-1, the operators ==, = 0 and time_hour

Chủ Đề