Passing numpy string-format arrays to fortran using f2py
Question: My aim is to print the 2nd string from a python numpy array in fortran, but I
only ever get the first character printed, and it's not necessarily the right
string either.

**Can anyone tell me what the correct way to pass full string arrays to
fortran?**

The code is as follows:

**testpy.py**

    
    
    import numpy as np
    import testa4
    
    strvar = np.asarray(['aa','bb','cc'], dtype = np.dtype('a2'))
    testa4.testa4(strvar)
    

**testa4.f90**

    
    
    subroutine testa4(strvar)
    implicit none
    
    character(len=2), intent(in) :: strvar(3)
    !character*2 does not work here - why?
    
    print *, strvar(2)
    
    end subroutine testa4
    

**Compiled with**

    
    
    f2py -c -m testa4 testa4.f90
    

**Output of above code**

    
    
    c
    

**Desired output**

    
    
    bb
    


Answer: Per the [documentation](http://docs.scipy.org/doc/numpy-dev/f2py/python-
usage.html#string-arguments), f2py likes string arrays to be passed with
dtype='c' (i.e., '|S1'). This gets you part of the way there, although there
are some oddities with array shape going on behind the scenes (e.g., in a lot
of my tests I found that fortran would keep the 2 character length, but
interpret the 6 characters as being indicative of a 2x6 array, so I'd get
random memory back in the output). This (as far as I could tell), requires
that you treat the Fortran array as a 2D character array (as opposed to a 1D
"string" array). Unfortunately, I couldn't get it to take assumed shape and
ended up passing the number of strings in as an argument.

I'm pretty sure I'm missing something fairly obvious, but this should work for
the time being. As to why CHARACTER*2 doesn't work ... I honestly have no
idea.

    
    
    MODULE char_test
    
    CONTAINS
    
    SUBROUTINE print_strings(strings, n_strs)
        IMPLICIT NONE
    
        ! Inputs
        INTEGER, INTENT(IN) :: n_strs
        CHARACTER, INTENT(IN), DIMENSION(2,n_strs) :: strings
    
    !f2py INTEGER, INTENT(IN) :: n_strs
    !f2py CHARACTER, INTENT(IN), DIMENSION(2,n_strs) :: strings
    
        ! Misc.
        INTEGER*4 :: j
    
    
        DO j=1, n_strs
            WRITE(*,*) strings(:,j)
        END DO
    
    END SUBROUTINE print_strings
    
    END MODULE char_test
    
    ----------------
    
    import numpy as np
    import char_test as ct
    
    strings = np.array(['aa', 'bb', 'cc'], dtype='c').T
    ct.char_test.print_strings(strings, strings.shape[1])
    
    strings = np.array(['ab', 'cd', 'ef'], dtype='c').T
    ct.char_test.print_strings(strings, strings.shape[1]) 
    
    -->python run_char_test.py
     aa
     bb
     cc
     ab
     cd
     ef
    

