CG_go_001_StringBasics



In [1]:
"A"


Out[1]:
A

In [2]:
"ACGT"


Out[2]:
ACGT

In [3]:
st := "ACGT"

In [4]:
len(st)


Out[4]:
4

In [5]:
""


Out[5]:


In [6]:
len("")


Out[6]:
0

In [7]:
import "math/rand"
st = "ACGT"
string(st[rand.Intn(len(st))]) // generating a random nucleotide


Out[7]:
C

In [8]:
string(st[rand.Intn(len(st))]) // repeated invocations might yield different nucleotides


Out[8]:
T

In [9]:
string(st[rand.Intn(len(st))]) // repeated invocations might yield different nucleotides


Out[9]:
T

In [10]:
string(st[rand.Intn(len(st))]) // repeated invocations might yield different nucleotides


Out[10]:
T

In [11]:
string(st[rand.Intn(len(st))]) // repeated invocations might yield different nucleotides


Out[11]:
C

In [12]:
// make random nucleotide string by concatenating random nucleotides
import "bytes"
var b bytes.Buffer
for i := 0; i < 40; i++ {
    b.WriteByte(st[rand.Intn(len(st))])
}
st = b.String()
st


Out[12]:
GCAAAGTGCAGTCCGTGAGTTTAGTCATTCACTCGCGGTC

In [13]:
st[1:3] // substring, starting at position 1 and extending up to but not including position 3
// note that the first position is numbered 0


Out[13]:
CA

In [14]:
st[0:3] // prefix of length 3


Out[14]:
GCA

In [15]:
st[:3] // another way of getting the prefix of length 3


Out[15]:
GCA

In [16]:
st[len(st)-3:len(st)] // suffix of length 3


Out[16]:
GTC

In [17]:
st[-3:] // another way of getting the suffix of length 3


repl.go:1:5: negative slice index: -3 == Expr{Type: int, Value: -3}

In [18]:
st1, st2 := "CAT", "ATAC"

In [19]:
st1


Out[19]:
CAT

In [20]:
st2


Out[20]:
ATAC

In [21]:
st1 + st2 // concatenation of 2 strings


Out[21]:
CATATAC