Scripting >> Python >> Examples >> Strings >> Functions (methods) for the string

 

Method Description Sample code
str.capitalize() Return a copy of the string S with only its first character
capitalized
>>> var="hello"
>>> var.capitalize()
'Hello'
str.format()

Return a formatted version of S, using substitutions from args and kwargs.  The substitutions are identified by braces ('{' and '}')

Syntax: str.format(*args, **kwargs) -> string

  • args = Positional parameters - list of parameters that can be accessed with index of parameter inside curly braces {index}
  • kwargs = Keyword parameters - list of parameters of type key=value, that can be accessed with key of parameter inside curly braces {key}

Example 1
>>> x=1
>>> y=2
>>> s=x+y
>>> print("{} + {} = {}".format(x,y,s))
1 + 2 = 3

Example 2
>>> print "{x} + {y} = {s}".format(x=1,y=2,s=x+y)
1 + 2 = 3