JavaPython:String

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti
Java vs Python

Java string literals are enclosed in double quotes ("), never in single quotes. Character literals are enclosed in single quotes ('), never in double quotes. A one-character string is not the same as the single character. Escape sequences (\n, \t, etc.) are single characters. There are no triple-quoted strings or raw strings in Java.

Strings can be concatenated with the + operator. Any object, of any type, may be concatenated to a String; it will be automatically converted to a String (for objects, via their toString() method) before being concatenated.

In Python, # starts a single-line comment; in Java, // starts a single-line comment. Java also has multi-line comments, beginning with /* and ending with */.

In Python a docstring (documentation string) is a string, usually triple-quoted, just after a function or method header. In Java, a Javadoc comment begins with /** and ends with */, and is put just before a method header, class header, or variable declaration.

In Python, string can reside in a pair of single quotes as well as a pair of double quotes. It supports multiplication: "x"*3 is "xxx".

Näide

Java Python
<syntaxhighlight lang="java" line="1" >

//string String city = "New York"; String state = "California";//has to be " not '

String lines = "multi-line " + "string"; </syntaxhighlight>

<syntaxhighlight lang="python" line="2" >
  1. Strings

city = "New York" state = 'California'

  1. multi-line string

lines = """multi-line string""" moreLines = multi-line string </syntaxhighlight>